Android >= 11 navigate between folder to find file without manage_external_storage permission

860 Views Asked by At

I have an app that have to update some machine firmware. Until targeting API 29 there are no problem, now Google Play Store need to target at least API30... and the struggle started.

The file manager picker I've used is no more compatible (unmaintained since February 2020), I've found a new one (arte programar material file picker) that works well but needs MANAGE_EXTERNAL_STORAGE permission (unless it navigates between folders but sees them all empty). Google doesn't allow the release of the app to the Play store because managing file is not the app's primary scope.

I need to let the user browse folders to search the zip file with the firmware update, select it and let the app open it and perform the update. This file could be everywhere on the device, is up to the user where to place it. The solution have to be compatible with Android 8+ (so since actual 12).

How can I solve this problem? Thanks in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

As suggested me by @blackapps, removing the MANAGE_EXTERNAL_STORAGE permission and switching to System file picker solved the problem.

On MainActivity

private void fileIntent() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("application/pdf");
}

On FilePicking

fun pickFirmwareFromStorage(context: Activity) {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "application/zip"

        // Optionally, specify a URI for the file that should appear in the
        // system file picker when it loads.
        val pickerInitialUri = "Download"
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(context, intent, PICK_FIRMWARE_REQUEST_CODE, null)
}