Brad Brad - 2 months ago 28
Android Question

Android run-time permissions -- user granting permission, not receiving it in onRequestPermissionsResult

I'm trying to set up my app to use run-time permissions. I've got the request dialog showing alright, but when I choose "Accept" on the dialog, and onRequestPermissionsResult is called, grantResults do not show PERMISSION_GRANTED, and the app doesn't get the permissions. Here is how I'm requesting the permission:

ActivityCompat.requestPermissions((Activity) contnext,
new String[]{Manifest.permission.MANAGE_DOCUMENTS, Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);


And the callback:

public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode){
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do good stuff
Log.i("net.bradmont.prayson", "Permissions granted by user");
} else {
Log.i("net.bradmont.prayson", "Permissions refused by user");
Log.i("net.bradmont.prayson", "grantResults.length " + grantResults.length);
for (int i = 0; i < grantResults.length; i++){
Log.i("net.bradmont.prayson", "grantResults[" + i +"]" + grantResults.length);
}
Snackbar snackbar1 = Snackbar.make(mCoordinator,
R.string.cant_open_file_no_permission, Snackbar.LENGTH_LONG);
snackbar1.show();
}

}
}


The result is that after accepting the permissions dialog, the app shows the error snackbar. Here's what shows in logcat:

net.bradmont.prayson I Permissions refused by user
I grantResults.length 2
I grantResults[0]2
I grantResults[1]2


I can't even figure out what 2 would signify in grantResults; the only consts with a value of 2 in the PackageManager documentation are COMPONENT_ENABLED_STATE_DISABLED and GET_RECEIVERS.

What on earth is going on here?

Answer

First, you're printing the length of int[] grantResults over and over in the loop instead of the values.

for (int i = 0; i < grantResults.length; i++){
      Log.i("net.bradmont.prayson", "grantResults[" + i +"]" + grantResults.length);
}

should be:

for (int i = 0; i < grantResults.length; i++){
      Log.i("net.bradmont.prayson", "grantResults[" + i +"]" + grantResults[i]);
}

Secondly, as CommonsWare stated Manifest.permission.MANAGE_DOCUMENTS is Protection level: signature, which means This permission cannot be granted to third-party apps. see the documentation for MANAGE_DOCUMENTS.

Since your first request is MANAGE_DOCUMENTS, not wonder the first value in grantResults is not PackageManager.PERMISSION_GRANTED. I'm going to guess the second value is actually PERMISSION_GRANTED corresponding to the Manifest.permission.READ_EXTERNAL_STORAGE request.

Long story short, get rid of the MANAGE_DOCUMENTS request:

ActivityCompat.requestPermissions((Activity) context,
     new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
     MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);