在我的应用程序中,我有一个列表,其中包含从标准android(棒棒糖)文件选择器中选择的图像。每个图像都有类似内容的URI://com.android.providers.media.documents/document/image%3A105628。
图像选择代码:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
通过单击调用ACTION_VIEW的列表意图中的图像。并查看应用程序崩溃异常。
图片查看代码:
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
viewIntent.setDataAndType(finalPhoto, "image/*");
startActivity(viewIntent);
例外:
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{247dd26c 18416:com.google.android.apps.plus/u0a51} (pid=18416, uid=10051) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
at android.os.Parcel.readException(Parcel.java:1540)
at android.os.Parcel.readException(Parcel.java:1493)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3267)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4589)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2439)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1442)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1064)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:921)
at android.content.ContentResolver.openInputStream(ContentResolver.java:646)
at jcq.a(PG:129)
at dcv.f(PG:28)
at dcv.j(PG:16)
at hxz.d(PG:40)
at df.e(PG:242)
at dg.a(PG:51)
at dg.c(PG:40)
at dx.call(PG:123)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
我读到了SAF,是否可以将我的应用授予的权限委托给另一个应用(观众)?
PS:我看到了这个解决方法。 Android KitKat securityException when trying to read from MediaStore
答案 0 :(得分:3)
答案是添加此标志intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
到ACTION_VIEW意图
答案 1 :(得分:0)
您应该将此添加到您的Manifest:
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
之后它应该工作
也许你可以看看这个答案: https://stackoverflow.com/a/20382826/2767703
P.S。 Android KitKat securityException答案没有帮助你吗?
答案 2 :(得分:0)
您应该将此添加到您的Manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
将此代码用于openGallery
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_CODE_GALLERY);