Home > Net >  Why onActivityResult intent is always null on getting all files access permission?
Why onActivityResult intent is always null on getting all files access permission?

Time:08-13

On getting all files access permission I want to pass extra data with intent to onActivityResult even if the user didn't grant permission but what I get there is always 0 as resultCode and null as Intent data.

The code for starting activity:

public void grantFilePermission(final String requestId) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        try {
            intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s", mContext.getApplicationContext(). getPackageName())));
        } catch (Exception e) {
            intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
        }
        intent.putExtra("requestId", requestId);
        mContext.startActivityForResult(intent, 2296);
    } else {
        ActivityCompat.requestPermissions(mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
    }
}

The ActivityCompat.requestPermissions part is not relevant here. mContext is defined as FullscreenActivity int other place.

And this is my onActivityResult method:

void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 2296 && data != null) {
        String requestId = data.getStringExtra("requestId");
    }
}

Here I always get 0 as resultCode and data is null, My main problem is with the data being null I need the requestId here even if the permission isn't granted.

CodePudding user response:

ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION is not documented to return a result. Hence, you cannot use it with startActivityForResult() very well, since its result will always be 0, and it will not contain any sort of result data.

I need the requestId here even if the permission isn't granted

You put that requestId on the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION Intent. Even for an action that does return a result Intent, that will be a different Intent than the one you used in startActivityForResult(). Random extras that you put on the outbound Intent are not copied into the result Intent.

  • Related