Android 6 permission to access the gallery.

I have questions about the resolution of the Android 6 environment. If the user wants to select a photo from the gallery, should we ask for permission READ_EXTERNAL_STORAGE? It looks like I could access the gallery, although I disabled storage permissions. Thanks.

+5
source share
2 answers

You need to request READ_EXTERNAL_STORAGE. You can access the gallery without it, but if you want to do anything with the media that you receive from the gallery, you will need READ permission.

A quick test of what happens in onActivityResult after the image has been selected in the gallery:

Disclaimer: reading com.android.providers.media.MediaProvider uri content: // media / external / images / media from pid = 8405, uid = 10177 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission ()

+4
source

For custom permissions, you can use runtime permissions if you use Android 6.0 or higher. This code can help you.

If your application does not already have the required permission, the application must call one of the requestPermissions () methods to request the appropriate permissions. Your application passes the permissions that it wants, and also the integer request code that you specify to identify this permission request. This method operates asynchronously: it returns immediately after the user responds to the dialog box, the system calls the application callback method with the results, passing the same request code that the application passed requestPermissions ().

// Here, thisActivity is the current activity if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { // Show an explanation to the user *asynchronously* -- don't block // this thread waiting for the user response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. } } 

To Know more about runtime permission

https://developer.android.com/training/permissions/requesting.html

0
source

Source: https://habr.com/ru/post/1235918/


All Articles