Check if READ_EXTERNAL_STORAGE requires permission to read from a specific Uri

I would like to check if I need Manifest.permission.READ_EXTERNAL_STORAGE to read from a given Uri.

This is required to properly handle incoming Intents in my application. For example, if I exchange an image with my app on WhatsApp and my app doesn't have read permission, that's OK. I can still read from this Uri as WhatsApp has given me permission to read this Uri. However, Google Photos shares the Uris as external storage of the Uri and reads it. I need to invite the user to give read permission to read from external storage.

I know I could always ask the user for permission to read my application, but I would like to avoid that whenever possible.

+3


source to share


2 answers


I'm not sure if this is the best way to handle this, but you can catch SecurityException

and then ask for the required permissions:



try {
    getContentResolver().query(uri, ...);
} catch (SecurityException e) {
    // Request storage perms here
    ActivityCompat.requestPermissions(this, new String[] {
        Manifest.permission.READ_EXTERNAL_STORAGE
    }, REQ_READ_STORAGE_PERMISSION);
}

      

+1


source


    if( PERMISSION_GRANTED != checkUriPermission( uri, android.os.Process.myPid(), android.os.Process.myUid(),
            Intent.FLAG_GRANT_READ_URI_PERMISSION)) {
        // request permission here
    }

      



0


source







All Articles