我有這些作爲我的權限:拒絕的權限從MediaDocumentsProvider
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我得到一個權限拒絕錯誤在這裏的openInputStream行:
if(bgUri != null && !bgUri.isEmpty()) {
try {
InputStream inputStream = context.getContentResolver().openInputStream(Uri.parse(bgUri)); // <-- error
return Drawable.createFromStream(inputStream, bgUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
我先用
載入我的圖片@Override
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (requestCode == SELECT_PHOTO && resultCode == Activity.RESULT_OK) {
try {
final Uri imageUri = imageReturnedIntent.getData();
final int takeFlags = imageReturnedIntent.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
mContext.getContentResolver().takePersistableUriPermission(imageUri, takeFlags);
然後保存圖像uri:
String backgroundURI = imageUri.toString();
int backgroundID = 0;
mStore.backgroundImageURI = backgroundURI;
mStore.backgroundImageNumber= backgroundID;
mStore.save();
堆棧跟蹤:
﹕ FATAL EXCEPTION: main
Process: com.myapp.pocketandroid, PID: 1284
java.lang.RuntimeException: Unable to resume activity {com.myapp.pocketandroid/com.myapp.pocketandroid.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{52946004 1284:com.myapp.pocketandroid/u0a60} (pid=1284, uid=10060) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{52946004 1284:com.myapp.pocketandroid/u0a60} (pid=1284, uid=10060) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2848)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4399)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2208)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1047)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
at com.myapp.pocketandroid.Utils.BackgroundImageManager.background(BackgroundImageManager.java:29)
at com.myapp.pocketandroid.BaseActivity.setBackground(BaseActivity.java:83)
at com.myapp.pocketandroid.BaseActivity.onResume(BaseActivity.java:44)
爲什麼我得到當我恢復活動,並嘗試加載的URI的MediaDocumentsProvider
許可崩潰?我認爲,通過獲得許可,我永久保留許可。
請發佈您的代碼,「試圖加載URI」以及完整的堆棧跟蹤。 – CommonsWare
@CommonsWare崩潰在第一塊代碼中。我添加了一個堆棧跟蹤。 – quantumpotato
你在調用'takePersistableUriPermission()'和'openInputStream()'之間做什麼?你寫道,當你「恢復[你的]活動」時你會遇到這個崩潰。你的意思是這個字面意思(即,它與你使用'takePersistableUriPermission()'是一樣的活動實例,只是回到前臺)?或者你的意思是別的嗎? – CommonsWare