0

写真を撮って保存する次のコードがあります。しかし、代わりに最終的な画像をSDカードに保存したいと思います。

public class TakePhoto extends Activity {

    ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);


        iv = (ImageView) findViewById(R.id.imageView1);

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bm = (Bitmap) data.getExtras().get("data");
        writeBitmapToMemory("image.png", bm);
        iv.setImageBitmap(bm);

    }

    public void writeBitmapToMemory(String filename, Bitmap bitmap) {
        FileOutputStream fos;

        try {
            fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();


        } 
        catch (IOException e) {
            e.printStackTrace();


        }

    }

再生しているように見えるビットが1つあります。SDカードに保存しようとしていますが、エラーでフォールオーバーします:(pastebin.com/FHihS4Wv)

onActivityResultを変更writeBitmapToMemory("/mnt/extSdCard/image.png", bm);しました–しかし、上記のpastebinnedエラーで失敗します

4

1 に答える 1

0

この質問によると、java.lang.IllegalArgumentException:パス区切り文字が含まれていStringますオブジェクトではなくオブジェクトFileを 使用してサブディレクトリにアクセスしようとしているため、これを取得しています

Fileファイル名を含む文字列ではなく、その特定の画像ファイルへのオブジェクトを関数に渡してみてください。

このようなもの:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    Bitmap bm = (Bitmap) data.getExtras().get("data");
    File imageFile = new File("image.png");
    writeBitmapToMemory(imageFile, bm);
    iv.setImageBitmap(bm);

}

public void writeBitmapToMemory(Filefile, Bitmap bitmap) {
    FileOutputStream fos;

    try {
        // fos = this.openFileOutput(file, Context.MODE_PRIVATE);
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();

    } 
....

編集:今は動作するはずですが、私はそれをテストしませんでした。

于 2012-09-04T22:18:21.963 に答える