35

我在捕获图像并将其从本机相机应用程序存储时遇到问题。这是我的一些代码的示例。

_path = Environment.getExternalStorageDirectory() + "make_machine_example.jpg";
File file = new File( _path );
Uri outputFileUri = Uri.fromFile( file );

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );

startActivityForResult( intent, 0 );

拍摄完照片并返回到原始活动后,当我通过 Android DDMS 文件资源管理器导航到我的 sd 卡时,图片不存在。任何人都知道为什么这没有被保存?

4

5 回答 5

28

如果有人仍在访问此线程,这是最终产品:

public class CameraCapture extends Activity {

    protected boolean _taken = true;
    File sdImageMainDirectory;

    protected static final String PHOTO_TAKEN = "photo_taken";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        try {

            super.onCreate(savedInstanceState);         
                    File root = new File(Environment
                            .getExternalStorageDirectory()
                            + File.separator + "myDir" + File.separator);
                    root.mkdirs();
                    sdImageMainDirectory = new File(root, "myPicName");


                startCameraActivity();
            }
        } catch (Exception e) {
            finish();
            Toast.makeText(this, "Error occured. Please try again later.",
                    Toast.LENGTH_SHORT).show();
        }

    }

    protected void startCameraActivity() {

        Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
        case 0:
            finish();
            break;

        case -1:

            try {
                StoreImage(this, Uri.parse(data.toURI()),
                        sdImageMainDirectory);
            } catch (Exception e) {
                e.printStackTrace();
            }

            finish();
            startActivity(new Intent(CameraCapture.this, Home.class));

        }

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
            _taken = true;
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);
    }

        public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
        Bitmap bm = null;
        try {
            bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
            FileOutputStream out = new FileOutputStream(imageDir);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            bm.recycle();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
于 2011-02-21T20:50:55.573 回答
21

您是否检查过 Environment.getExternalStorageDirectory() 的输出是什么,因为如果它不包含尾随文件分隔符 (/),那么您的图像将最终位于不驻留在 SD 卡上的目录中,例如:

 /mnt/sdcardmake_machine_example.jpg

当你真正想要的是:

 /mnt/sdcard/make_machine_example.jpg

试试这个代码:

 _path = Environment.getExternalStorageDirectory() + File.separator +  "make_machine_example.jpg";
于 2010-08-16T07:18:56.260 回答
14

1. 只需使用

new File(Environment.getExternalStorageDirectory(),  "make_machine_example.jpg");

并且不要打扰分隔符。

2. 之前也遇到过同样的问题。SenseUI 手机有一个不创建文件的自定义相机应用程序。你用的是什么设备?它可能已经在最新的设备中得到修复,但它也可能仍然是一个问题。所以这里有一个完整的示例,如何克服将照片保存到文件的问题

于 2010-08-18T06:31:11.647 回答
1

您应该在保存图像后执行媒体扫描

 sendBroadcast(new Intent(
            Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
于 2012-10-26T13:30:22.003 回答
-6

将此行添加到 AndroidManifest.xml 文件中并删除扩展名 make_machine_example:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

它将开始捕获照片并存储到 SD 卡中。

于 2011-04-21T06:11:45.137 回答