androidandroid-intentstorage-access-frameworkandroid-storage

SAF: No Activity found to handle Intent - android.intent.action.OPEN_DOCUMENT_TREE - Android 13


Just updated the app which works now only with SAF so users would be able to choose any directory they wish and app could work not just with media files (MediaStore is a useless thing).

But interesting thing I got in Firebase Crashlytics:

Fatal Exception: android.content.ActivityNotFoundException No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT_TREE (has extras) }

for Galaxy A52 device on Android 13

Didn't expect that such things can be missing, especially on Android 13 devices.

How am I supposed to handle such errors? Can I even help users to fix such issues, mb suggest them to install one of File Browser apps from Goolge Play, will it help to choose directory with android.intent.action.OPEN_DOCUMENT_TREE for my app?

fun selectFolderSaf(
    activity: Activity,
    coroutineScope: CoroutineScope,
    activityResultLauncher: ActivityResultLauncher<Intent>,
) {
    val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        val storageManager =
            activity.getSystemService(Context.STORAGE_SERVICE) as StorageManager
        storageManager.primaryStorageVolume.createOpenDocumentTreeIntent()
    } else {
        Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
    }

    with(intent) {
        putExtra("android.content.extra.SHOW_ADVANCED", true)
        putExtra("android.content.extra.FANCY", true)
        putExtra("android.content.extra.SHOW_FILESIZE", true)
    }
    coroutineScope.launch {
        activityResultLauncher.launch(intent)
    }
}

Solution

  • Didn't expect that such things can be missing, especially on Android 13 devices.

    Those Intent actions are not always supported, especially on atypical form factors. Neither Android TV nor Wear OS reliably support them, for example.

    for Galaxy A52 device on Android 13

    Technically speaking, for Android 13, your code is not directly using ACTION_OPEN_DOCUMENT_TREE. You are using the result of storageManager.primaryStorageVolume.createOpenDocumentTreeIntent(). It is possible that Samsung did not adequately test this particular bit of their Android integration.

    How am I supposed to handle such errors?

    Fall back to a plain ACTION_OPEN_DOCUMENT_TREE request if the storageManager.primaryStorageVolume.createOpenDocumentTreeIntent() launch throws an ActivityNotFoundException.

    If that too fails, say "sorry, your device is incompatible".

    Can I even help users to fix such issues

    Not generally.

    mb suggest them to install one of File Browser apps from Goolge Play, will it help to choose directory with android.intent.action.OPEN_DOCUMENT_TREE for my app?

    No.