【发布时间】:2022-02-21 06:08:00
【问题描述】:
我有 AddActivity,它可以让您从可以从相机拍摄的图片或可以从图库中选择的图像中获取 URI。然后你可以去DetailsActivity查看图像。我现在可以正常工作,直到您重新启动设备。重新启动并尝试转到该图像的 DetailsActivity 后,出现以下错误:
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{3a5e86d 2915:jeremy.com.wineofmine/u0a321} (pid=2915, uid=10321) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
我访问了"Open Files Using Storage Access Framework" Android Development 页面并阅读了 Persist Permissions 部分。不过,我无法将它应用到我的项目中。
我认为我不明白的主要事情是您似乎需要调用一个意图(在我的情况下,在 DetailsActivity 中),但我什至没有意图。
这是让您选择图库图片的意图。这是在 AddActivity 中:
Intent intentGallery = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intentGallery.addCategory(Intent.CATEGORY_OPENABLE);
intentGallery.setType("image/*");
intentGallery.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentGallery.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intentGallery, SELECT_IMAGE);
在 DetailsActivity 中,这是它实际崩溃的地方:
imageURI = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE)));
bitmap = null;
try {
//If the cursor does not have anything in the image column, set the image to null, with a height so the textviews look decent
if (cursor.isNull(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE))){
mFullImage.setImageBitmap(null);
mFullImage.setMaxHeight(300);
}else{
//remake the bitmap from the URI in the image column
//********This next line is where the program crashes**********
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
mFullImage.setImageBitmap(bitmap);
}
我可以得到一些帮助来弄清楚如何将它应用到我的项目中吗?
【问题讨论】:
-
从使用的意图中删除 addFlags() 开始。没有意义。
-
之后你应该在 onActivityResult 中获得一个持久化的 uri 权限。你的代码在哪里?
-
imageURI = Uri.parse(cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_IMAGE)));。我的上帝...我们应该知道你在那里带什么uri吗?你应该使用每个人都能理解的代码。 -
@greenapps 抱歉,我只是不认为该行的结果对任何人都有帮助。也许我弄错了?这是从该行检索的日志标签:“DetailsActivity: imageURI: content://com.android.providers.media.documents/document/image%3A46421”
-
是的,这样更好。您应该立即在代码中使用它。