权限被拒绝:MediaDocumentsProvider
尝试显示URI时出现以下异常。我认为这是在我的活动停止后发生的,我尝试再次访问可行的URI。还有其他问题可以解决这个问题,但是我对如何对我的代码应用任何解决方案感到非常困惑,因为我的takePhotoIntent允许拍照或从图库中选择图片(如下所示)。
Unable to open content: content://com.android.providers.media.documents/document/image%3A49688java.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:
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);
}
}
}
回答:
根据Storage Access
Framework客户端文档:
*
如果您希望您的应用仅读取/导入数据,请使用ACTION_GET_CONTENT。通过这种方法,应用程序可以导入数据的副本,例如图像文件。
*
如果您希望您的应用对文件提供者拥有的文件具有长期,持久的访问权限,请使用ACTION_OPEN_DOCUMENT。一个示例是照片编辑应用程序,该应用程序允许用户编辑存储在文档提供程序中的图像。
在使用ACTION_GET_CONTENT
时,销毁组件后,对Uri的访问将终止(除非您将Uri传递给新组件)。如果您想在组件的有效期内继续访问它,或者切换到ACTION_OPEN_DOCUMENT
并保留权限(仅适用于Uris文档),必须从Uri复制该文件。
以上是 权限被拒绝:MediaDocumentsProvider 的全部内容, 来源链接: utcz.com/qa/425062.html