koras
koras

Reputation: 1107

Runtime Permissions: no permission immediately after granting

I'm trying to get a storage permission in Marshmallow. Everything seems to work great and I can grant or deny a permission.

private static final int REQUEST_STORAGE = 1;

// request storage permission
private void requestStoragePermission() {
    String[] permissions = new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE };
    if (!hasStoragePermission()) {
        Log.e(TAG, "No storage permission at the moment. Requesting...");
        ActivityCompat.requestPermissions(this, permissions, REQUEST_STORAGE);
    } else
        Log.e(TAG, "We already have storage permission. Yay!");
}

// check is storage permission granted
private boolean hasStoragePermission() {
    String storagePermission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    int hasPermission = ContextCompat.checkSelfPermission(this, storagePermission);
    return (hasPermission == PackageManager.PERMISSION_GRANTED);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_STORAGE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.e(TAG, "Storage permission granted");
            } else {
                Log.e(TAG, "Storage permission denied");
                Toast.makeText(getApplicationContext(), getString(R.string.no_storage_permission), Toast.LENGTH_SHORT).show();
            }
            break;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

However the problem is here. When I'm using this feature for the first time, hasStoragePermission() is always false, even after granting the permission. When the permission wasn't granted before I have to use the context menu twice to be able to do what I want. It's unacceptable.

// some code...
// even more code...

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case ID_CONTEXT_MENU_SAVE_IMAGE:
            // in order to save anything we need storage permission
            requestStoragePermission();
            Log.e("SomeActivity", "storagePermissionGranted: " + hasStoragePermission());
            if (!hasStoragePermission())
                return false;
            saveImageToDisk(mPendingImageUrlToSave);
            break;
    }
    return super.onContextItemSelected(item);
}

Is there any known bug in Android Marshmallow which could cause it? The code seems to be ok because in fact I can grant / deny storage permission. I want to have it immediately though.

Upvotes: 2

Views: 1198

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

The request for permissions and the returned result are asynchronous. Once your onRequestPermissionsResult() is executed your app is either granted or denied the permission. In some cases this will cause your entire app to be restarted (certain permissions.) FYI, external storage should be one of those, but right now M has an issue where the app isn't always restarted when granting this permission.

Upvotes: 2

Related Questions