问题描述
尝试显示URI时出现以下异常。我认为它发生在我的活动停止后,我尝试再次访问一个可行的URI。还有其他问题可以解决这个问题,但我对如何将任何解决方案应用于我的代码感到困惑,因为我的takePhotoIntent允许从图库中获取或选择图片(见下文)。
I am getting the following exception when trying to display a URI. I think it occurs after my activity is stopped and I try to access a viable URI again. There are other questions addressing this problem but I am very confused as to how to apply any solutions to my code since my takePhotoIntent allows for a picture to either be taken or chosen from gallery (found below).
Unable to open content: content://com.android.providers.media.documents/document/image%3A49688
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri
content://com.android.providers.media.documents/document/image%3A49688 from pid=821, uid=10238 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
我的createImageFile和我的takePhotoIntent:
My createImageFile and my takePhotoIntent:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp;
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
for(int i = 0; i < 4; i++) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
outputFileUri = Uri.fromFile(photoFile);
} catch (IOException ex) {
Log.w("error","IOException");
}catch (NullPointerException nullEx) {
Log.w("error","NullPointerException");
}
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
if(id.equals(HAPPY_ID))
startActivityForResult(chooserIntent, REQUEST_HAPPY_PHOTO);
if(id.equals(SURPRISED_ID))
startActivityForResult(chooserIntent, REQUEST_SURPRISED_PHOTO);
if(id.equals(AFRAID_ID))
startActivityForResult(chooserIntent, REQUEST_AFRAID_PHOTO);
if(id.equals(UPSET_ID))
startActivityForResult(chooserIntent, REQUEST_UPSET_PHOTO);
if(id.equals(SAD_ID))
startActivityForResult(chooserIntent, REQUEST_SAD_PHOTO);
}
}
}
推荐答案
根据 :
使用如果您希望您的应用对文档提供者拥有的文档具有长期,持久的访问权限。一个例子是照片编辑应用程序,它允许用户编辑存储在文档提供程序中的图像。
Use ACTION_OPEN_DOCUMENT if you want your app to have long term, persistent access to documents owned by a document provider. An example would be a photo-editing app that lets users edit images stored in a document provider.
当您使用 ACTION_GET_CONTENT
时,您对Uri的访问在组件被销毁时结束(除非您将Uri传递给新组件)。如果您希望在组件的生命周期之后继续访问该文件,则必须从Uri复制该文件,或者切换到 ACTION_OPEN_DOCUMENT
和(仅限文档Uris)。
As you are using ACTION_GET_CONTENT
, your access to the Uri ends when the component is destroyed (unless you pass the Uri to a new component). You must make a copy of the file from the Uri if you'd like to continue to access it beyond the lifetime of your component or switch to ACTION_OPEN_DOCUMENT
and persist permissions (possible only with document Uris).
这篇关于权限拒绝:MediaDocumentsProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!