@wangwangheng
2015-01-29T06:00:02.000000Z
字数 2060
阅读 10531
问题解决
使用如下的代码开启相机拍照:
Uri mCaptureUri = getActivity().getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, mCaptureUri);getActivity().startActivityForResult(intent, REQUEST_CAPTURE);
报错:
01-29 12:37:49.635: E/Parcel(24434): Reading a NULL string not supported here.01-29 12:38:01.578: E/AndroidRuntime(24434): FATAL EXCEPTION: main01-29 12:38:01.578: E/AndroidRuntime(24434): java.lang.UnsupportedOperationException: Unknown URI: content://media/external/images/media01-29 12:38:01.578: E/AndroidRuntime(24434): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:169)01-29 12:38:01.578: E/AndroidRuntime(24434): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)01-29 12:38:01.578: E/AndroidRuntime(24434): at android.content.ContentProviderProxy.insert(ContentProviderNative.java:420)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:
public void startCamera() throws IOException {Log.d("TDM_CAMERA", "Starting camera on the phone...");File photosDir = new File(Environment.getExternalStorageDirectory(), "photos");if (!photosDir.isDirectory()) {photosDir.mkdirs();}File imageFile = File.createTempFile("testphoto", ".jpg", photosDir);Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);startActivityForResult(intent, 1337);}
Also make sure that using external storage is declared in your Android manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><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