Grant uri permission to uri in EXTRA_STREAM in intent
Asked Answered
D

1

16

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

Demonetize answered 19/7, 2013 at 15:48 Comment(3)
Did you ever find an answer to this question? I have the same problem trying to grant read uri permission to data in the EXTRA_STREAMDisaffirm
Correct me if I am wrong. You are trying to pass an Uri (pointing file in private filesystem) to another app installed on device. Uri is accessed through your ContentProvider.Widow
Please post your manifest and the part of your code where you create Intent and start activity.Widow
W
2

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

Widow answered 20/5, 2014 at 11:14 Comment(2)
My requirement is to provide attachment to Email app using content provider which has exported flag set to false. First activity which gets started by email intent is the chooser activity and for startActivityForResult will not return me finish of email activity instead it will give me finish of ChooserActivity. So if at that moment permissions are revoked than email app will fail to read the attachment.Imprimatur
Are you calling ChooserActivity explicitly? If yes, then maybe try withoutit . If you want to be sure that only email apps are shown call this intent.setData(Uri.parse("mailto:"));. If no, then maybe <grant-uri-permission> for your provider will be sufficent. It allows to specify files that can be accessed via ContentProvider(works with regex). If it still not good ask separate question, paste your code and let me know. I will figure out something. Thumbs up!Widow

© 2022 - 2024 — McMap. All rights reserved.