Grant uri permission to uri in EXTRA_STREAM in intent

android, android-contentprovider, android-permissions

Solution

I did some reading about it. First, EXTRA_STREAM does not use `FLAG_GRANT_READ_URI_PERMISSION`. It works starting from JellyBean only because under the hood, calling `startActivity()` copies `EXTRA_STREAM` to `ClipData`, which is set for Intent and uses `FLAG_GRANT_READ_URI_PERMISSION`.

Regarding your question where revokeUriPermission() should be called.

I suggest to use:

private static final int REQUEST_CODE = 1;
startActivityForResult(intent, REQUEST_CODE);

instead of

startActivity(intent);

and then override the following method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == REQUEST_CODE) {
        // Call revokeUriPermission here

    }
}

Above method will be called when started activity exits. Have a look at documentation here

Problem

With `FLAG_GRANT_READ_URI_PERMISSION` in intent that passed to `startActivity`, we can grant `Uri` permission if the uri is set using `setData`. But if the `Uri` in put in `EXTRA_STREAM`, the `Uri` is not granted before jeallybean. I know we can use `grantUriPermission` followed by a `revokeUriPermission` to cancel back the permission granted. But it seems that there is no good place to run `revokeUriPermission`. Is there any better solution? Or any suggestion to put `revokeUriPermission`? Thanks in advance. Related link: How to grant temporary access to custom content provider using FLAG_GRANT_READ_URI_PERMISSION

Original source

Related problems