-3

I want to get bitmap by uri by:

 MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(uri))

but this code throw a exception says:

java.lang.SecurityException: Permission Denial: opening provider 
com.android.providers.media.MediaDocumentsProvider from 
ProcessRecord{430b1748 29271:com.x.x.x/u0a88} (pid=29271, uid=10088) 
requires android.permission.MANAGE_DOCUMENTS or 
android.permission.MANAGE_DOCUMENTS

and My build gradle:

compileSdkVersion 26
minSdkVersion 23
targetSdkVersion 23

I know that I need to ask for permission dynamically.

I have this In my fragment:

int hasWriteContactsPermission = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.MANAGE_DOCUMENTS);
if (hasWriteContactsPermission == PackageManager.PERMISSION_GRANTED) {
    generateGridView();
}
else {
    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_PHONE_STATE}, MANAGE_DOCUMENTS_REQUEST_CODE);
}

I debug this code, And I found that requestPermissions can not trigger the dialog to show for asking permission. And -1 was returned as grantResults in onRequestPermissionsResult.

In my only manifest I have:

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

actually, I know there's a same question there, But I have no reputation to comment and ask more there!! I have the same code as the accepted answer, but does not solve the problem. This really driven me crazy. SO is really unfriendly to normal coder that only sometime want to ask!

Banana
  • 2,435
  • 7
  • 34
  • 60
  • you should check this tutorial: https://github.com/codepath/android_guides/wiki/Accessing-the-Camera-and-Stored-Media#accessing-stored-media – JohnnyAW Feb 26 '18 at 12:56
  • Where is `uri` coming from? How did you get it originally, and are you doing anything with it (e.g., saving it to disk, passing it between activities)? – CommonsWare Feb 26 '18 at 13:06
  • @CommonsWare Yes, I get this uri by pick one image from gallery then I save it to disk. If I can not get a image by uri, How can I save the path of a image and retrieve the image agian? thanks. – Raymond Guo Feb 26 '18 at 13:11
  • "I get this uri by pick one image from gallery then I save it to disk" -- that will not work. Think of a `Uri` as being akin to a URL to a password-protected Web site. Once the Web session ends, that URL is useless. Same thing here: you will lose access to the content identified by that `Uri` after your process ends. There is no "path to the image", because the image does not need to be a file. You will need to copy the image content into a file that you manage, or use `ACTION_OPEN_DOCUMENT` on Android 4.4+ and use `takePersistableUriPermission()` to have durable access to the content. – CommonsWare Feb 26 '18 at 13:17
  • See https://commonsware.com/blog/2016/08/10/uri-access-lifetime-shorter-than-you-might-think.html and https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html and https://commonsware.com/blog/2016/05/03/storage-access-framework-faq.html for more on all of this. – CommonsWare Feb 26 '18 at 13:22
  • @CommonsWare thank you. I will try to copy the image to a certain file. – Raymond Guo Feb 26 '18 at 13:31

1 Answers1

2

MANAGE_DOCUMENTS permission has signature protection level. As a third party app you cannot get it.

laalto
  • 150,114
  • 66
  • 286
  • 303