Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Marshmallow permission model on OS 4.0 READ_EXTERNAL_STORAGE permission always not granted

I have a problem when running my app on Android OS 4.0 and requesting READ_EXTERNAL_STORAGE permission with:

ActivityCompat.requestPermissions(ctx, requestedPermissions, requestCode);

I always get on the callback

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
        @NonNull int[] grantResults) { 

grantResults != PackageManager.PERMISSION_GRANTED

Checking the permission with ActivityCompat.checkSelfPermission is always returning permissionDenied.

It works well on OS 6.0 by requesting the permission with system dialog. Android OS 4.x excepted the 4.0 the permission is always granted. OS 4.0 the other permissions (Camera,Calendar,Contact,Phone) are behaving well except the READ_EXTERNAL_STORAGE causing this issue.

Maybe an OS issue?

like image 479
skirix Avatar asked Nov 02 '15 16:11

skirix


People also ask

How do I grant permission to write external storage?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

How do I allow management of all files android 11 permission programmatically?

On the Settings > Privacy > Permission manager > Files and media page, each app that has the permission is listed under Allowed for all files. If your app targets Android 11, keep in mind that this access to "all files" is read-only.


1 Answers

What ActivityCompat.requestPermissions() does is:

  • call through to the real requestPermissions() if you are on Android 6.0+, or

  • use PackageManager to see if you hold the requested permissions on older versions of Android

The problem with READ_EXTERNAL_STORAGE is that it was added in API Level 16 (Android 4.1). You cannot hold it on older versions of Android than that, for the simple reason that it did not exist.

Either:

  • Set your minSdkVersion to 16, or

  • Put your own logic in to handle this case, recognizing that READ_EXTERNAL_STORAGE is irrelevant prior to API Level 16

like image 131
CommonsWare Avatar answered Oct 01 '22 05:10

CommonsWare