[关闭]
@wangwangheng 2015-01-29T06:00:02.000000Z 字数 2060 阅读 9987

Android使用相机拍照报错:java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media

问题解决


使用如下的代码开启相机拍照:

  1. Uri mCaptureUri = getActivity().getContentResolver()
  2. .insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
  3. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  4. intent.putExtra(MediaStore.EXTRA_OUTPUT, mCaptureUri);
  5. getActivity().startActivityForResult(intent, REQUEST_CAPTURE);

报错:

  1. 01-29 12:37:49.635: E/Parcel(24434): Reading a NULL string not supported here.
  2. 01-29 12:38:01.578: E/AndroidRuntime(24434): FATAL EXCEPTION: main
  3. 01-29 12:38:01.578: E/AndroidRuntime(24434): java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media
  4. 01-29 12:38:01.578: E/AndroidRuntime(24434): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:169)
  5. 01-29 12:38:01.578: E/AndroidRuntime(24434): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
  6. 01-29 12:38:01.578: E/AndroidRuntime(24434): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)
  7. 01-29 12:38:01.578: E/AndroidRuntime(24434): at android.content.ContentResolver.insert(ContentResolver.java:866)

部分机型会导致Crash,部分机型不会Crash但是返回的Uri是null

搜索Stack Overflow之后得到解决办法(实际上是换一种实现方式):
The way you set the output file is fishy.
Try like this:

  1. public void startCamera() throws IOException {
  2. Log.d("TDM_CAMERA", "Starting camera on the phone...");
  3. File photosDir = new File(Environment.getExternalStorageDirectory(), "photos");
  4. if (!photosDir.isDirectory()) {
  5. photosDir.mkdirs();
  6. }
  7. File imageFile = File.createTempFile("testphoto", ".jpg", photosDir);
  8. Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  9. intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
  10. intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
  11. startActivityForResult(intent, 1337);
  12. }

Also make sure that using external storage is declared in your Android manifest:

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

For more info see this page in the docs: http://developer.android.com/training/camera/photobasics.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注