2

My android application opens the "document tree" selector to prompt read permission on an external storage full directory (as per android 10+ scoped storage rules).

Relative Code

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
    intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    startActivityForResult(intent, REQUEST_ID_PERMISSION);

Works fine for all tested versions and device variants. But, my crashlytics shows the below error ocuring a considerable number of times for some Samsung devices. (A20 - Android 9 | S10/10+ - Android 10 & 11 | Note20 - Android 11)

Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT_TREE flg=0x40 }
   at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2071)
   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1717)
   at android.app.Activity.startActivityForResult(Activity.java:5252)
   at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)

Any potential reason and solution for this problem, knowing that:

  1. I can check the availability of the activity prior to launching the OPEN_DOCUMENT_TREE, but this will result in the document selector not opening.
  2. Some old answers suggest adding intent.setType("*/*"), but this is no longer valid, and causes crashes with reasons explained in another questions (That i can no longer find, but is irrelevant here).
  3. I also think it might be very likely related to the fact that some users have disabled the FILES app on their devices, since MOST PROBABLY the error is not occurring on all the devices models listed above, but for a subset.

thank you.

Elio Khattar
  • 320
  • 1
  • 3
  • 16
  • 1
    `intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)` Dont put that flag. You cannot grant anything. Instead you should be glad if in onActivityResult permissions are granted to you. – blackapps May 09 '21 at 09:23
  • `but this will result in the document selector not opening.` Of course. You will not launch the intent then. And instead you display a nice Toast() to inform the user. And tell them all about the Files app. – blackapps May 09 '21 at 09:27
  • `think it might be very likely related to the fact that some users have disabled the FILES app on their devices` But then.. you can easily test that with your own device. – blackapps May 09 '21 at 09:28
  • @blackapps,. For the FLAG_GRANT_PERSISTABLE_URI_PERMISSION, it is used whenever the required path is granted to the application, so i can persist the permission grant, and no longer ask for it, unless it was explicitly removed again by either the user or android :D, thanks. – Elio Khattar May 09 '21 at 10:18
  • "so i can persist the permission grant, and no longer ask for it, unless it was explicitly removed again by either the user or android" -- except that it does not go on the outbound `Intent`. Ordinary apps do not use this flag; it is used by document providers to offer persistable permissions to apps. "I also think it might be very likely related to the fact that some users have disabled the FILES app on their devices" -- that's a possibility, and one you could probably test. – CommonsWare May 09 '21 at 11:21
  • @CommonsWare would it be please possible to further elaborate your comment about "it is used by document providers..." or provide a certain link where i can further check. thank you. – Elio Khattar May 09 '21 at 13:44
  • Well, we can start with [the JavaDocs](https://developer.android.com/reference/android/content/Intent#FLAG_GRANT_PERSISTABLE_URI_PERMISSION), which says "This flag only offers the grant for possible persisting; the receiving application must call ContentResolver#takePersistableUriPermission(Uri, int) to actually persist." Your app is the receiving application in this context. The supplying application -- the documents provider -- is the one that uses this flag. – CommonsWare May 09 '21 at 13:47
  • There is also [this bit of documentation on persisting permissions](https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions), which shows `takePersistableUriPermission()` and does not show `FLAG_GRANT_PERSISTABLE_URI_PERMISSION`. – CommonsWare May 09 '21 at 13:49

1 Answers1

0

Data point: I came across this discussion while searching the web for an explanation of a similar exception / crash reported by a user of an app of mine (on a Samsung S10). I pointed the user to this discussion, and sure enough, he had deactivated the Files app, and reactivating it solved the problem.

atrocia6
  • 319
  • 1
  • 8