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

Community
  • 1
  • 1
Bear
  • 5,138
  • 5
  • 50
  • 80
  • 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_STREAM – StuStirling Feb 10 '14 at 15:34
  • 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. – Damian Petla May 20 '14 at 10:19
  • Please post your manifest and the part of your code where you create Intent and start activity. – Damian Petla May 20 '14 at 10:45

1 Answers1

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

JM Lord
  • 1,031
  • 1
  • 13
  • 29
Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • 1
    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. – AndRSoid May 21 '14 at 06:27
  • 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 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! – Damian Petla May 21 '14 at 10:58