5

My app use camera to take a photo and use it for long term.

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri resultUri = null;
resultUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
      new ContentValues());
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, resultUri);
startActivityForResult(imageCaptureIntent, IMAGE_CAPTURE_REQUEST_CODE);

But when I call takePersistableUriPermission(), I will get SecurityException: No persistable permission grants found

I've read this Getting Permission Denial Exception. It works perfect to ACTION_OPEN_DOCUMENT. How do I get a persistent permission from Camera?

Community
  • 1
  • 1
Mingwei Lin
  • 140
  • 1
  • 10
  • 1
    You have the URI - why are you do need to persist access at all? – ianhanniballake Jul 22 '16 at 20:17
  • @ianhanniballake Wow. You're right. After reboot, uri from camera it can be accessed without persistent permission. But, why does it fail to uri from ACTION_GET_CONTENT if I don't call takePersistableUriPermission()? – Mingwei Lin Jul 22 '16 at 22:18

1 Answers1

8

Access to MediaStore URIs are only controlled by the storage permissions (i.e., READ_EXTERNAL_STORAGE) so as long as you continue to hold the storage permission, you can access the Uris so in this case you don't need to persist permissions at all.

URI based permissions, used in ACTION_GET_CONTENT, ACTION_OPEN_DOCUMENT, etc. give special one time access to a URI via the FLAG_GRANT_READ_URI_PERMISSION being included with the returned Intent.

It is only document URIs (ones where DocumentsContract.isDocumentUri() returns true) that allow you to persist permissions to give more permanent access to a Uri.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks for explanation. That helps a lot. – Mingwei Lin Jul 26 '16 at 20:20
  • 1
    Not exactly. If you ask a user to choose a file using `Intent.ACTION_GET_CONTENT` - the URI you receive will successfully pass `DocumentsContract.isDocumentUri()` but will fail if you try to save permission calling `takePersistableUriPermission()` with exception `SecurityException: No persistable permission grants found for //UID 10829 and Uri content://...` ; You should open file chooser with `Intent.ACTION_OPEN_DOCUMENT`. – Kirill Karmazin Feb 08 '19 at 21:56