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.

8
  • 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 9, 2021 at 9: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 9, 2021 at 9: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 9, 2021 at 9: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. May 9, 2021 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. May 9, 2021 at 11:21

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.