On some devices ACTION_OPEN_DOCUMENT_TREE doesn't show the chooser

809 views Asked by At

I use ACTION_OPEN_DOCUMENT_TREE to allow the user to choose a path and get SAF permissions, but intent ACTION_OPEN_DOCUMENT_TREEon some user devices results in messages like

No app available to complete this action

and similar messages or the opening of an empty activity chooser.

The API level of the affected users OS are between API level 23 and 25 (at the moment).

I thought this was an operation supported by default on Android versions above Lollipop, but seems I was wrong.

Is there any alternative other than bloat the App with a double file management one based on SAF and one based on the classic direct file-system access?

1

There are 1 answers

0
user924 On

I hope it happens only for older version of Android which still can work with WRITE_EXTERNAL_STORAGE permission.

In this case we still can work with SAF and its DocumentFile

Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).canBeHandled(packageManager)

fun Intent.canBeHandled(packageManager: PackageManager): Boolean {
    return resolveActivity(packageManager) != null
}

If ACTION_OPEN_DOCUMENT_TREE is not supported then you can ask users to grant WRITE_EXTERNAL_STORAGE permission, select some folder (File) to work with and then convert it to DocumentFile using DocumentFile.fromFile(File) and returned DocumentFile will have Write permission just like File but it only works for devices which still fully support WRITE_EXTERNAL_STORAGE

So all other logic in your app can be implemented using DocumentFile. The problem is just getting initial parent folder to work with (only in this case you will need File and WRITE_EXTERNAL_STORAGE if ACTION_OPEN_DOCUMENT_TREE is not supported)

WRITE_EXTERNAL_STORAGE is deprecated (and is not granted) when targeting Android 13+

p.s. we can use WRITE_EXTERNAL_STORAGE up to Android 12 (SDK 32) with android:requestLegacyExternalStorage="true" flag for application at the manifest