2

I want to use the Storage Access Framework (SAF) in Android to get and save in a local database the uri of a large amount of files (>600).

So to get 1 file's uri, I call:

intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                | Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
                | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, RESULT_FILE_OPEN)

then in the onActivityResult, I persist the permission granted:

getContentResolver().takePersistableUriPermission(intent.getData(), Intent.FLAG_GRANT_READ_URI_PERMISSION);

But I know Android 11 limits to 512 the permissions that you can persist. So I thought I could use the flag Intent.FLAG_GRANT_PREFIX_URI_PERMISSION to grant access to files whose uri has a prefix match thus reducing the number of persisted uri permissions.

But I have no clue on how to use this flag. The way I used it, I can't get read access to the files with a prefix match.

So my question is:

How the flag Intent.FLAG_GRANT_PREFIX_URI_PERMISSION is supposed to be used?

Simon
  • 1,890
  • 19
  • 26
  • You better start to remove all flags from your intent as they do not make sense there. – blackapps Jun 24 '21 at 08:02
  • Why do you want >600 if 128 and 512 are the limits? And why yet store in a database where you can always determine them without? – blackapps Jun 24 '21 at 08:08
  • If you want to do something with that prefix flag then while taking the permission i would say. But never done do so... – blackapps Jun 24 '21 at 08:11
  • Thank you for CommonsWare blog link. I was unaware of this limitation. – blackapps Jun 24 '21 at 08:12
  • @blackapps, then what are the purpose of these flags? – Simon Jun 25 '21 at 02:47
  • You can use them in for example onActivityResult to take persistable uri permissions (as you do, so why ask?). And use them with ACTION_VIEW to serve a readable file. – blackapps Jun 25 '21 at 06:15
  • @blackapps you mean like this: getContentResolver().takePersistableUriPermission(intent.getData(), Intent.FLAG_GRANT_READ_URI_PERMISSION)? Because if I do like this: takePersistableUriPermission(intent.getData(), Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) it gives an error! – Simon Jun 25 '21 at 12:42
  • Is it intentionally that you do not post complete code (in your post, not in a commenr) and not mention the error? – blackapps Jun 25 '21 at 13:41

0 Answers0