Search code examples
androidpermissions

WRITE and READ permissions in Android 14


I have this function in my app:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            try {
                Intent intent = new Intent("android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION");
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse(String.format("package:%s", new Object[]{getApplicationContext().getPackageName()})));
                startActivityForResult(intent, 200);
            } catch (Exception e) {
                Intent intent = new Intent();
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", getApplicationContext().getPackageName(), null));
                startActivityForResult(intent, 200);
            }
        } else {
            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
            ActivityCompat.requestPermissions(this, permissions, 1);
        }

But the MANAGE_APP_ALL_FILES_ACCESS_PERMISSION is not allowed for the use of my application. The problem is that I must ask the user for READ and WRITE permissions to the shared storage (Documents) in order to access backup files... but I can't find the correct way to do it in Android 14.

Could you tell me what permissions or how to access the files in the DOCUMENTS folder on the external storage?

Thank you so much.


Solution

  • If the files are created by your app then you have read and write access to them without needing any permission.

    If the files are not created by your app use ACTION_OPEN_DOCUMENT to let the user select a file.

    If your app wants to create a file in Documents folder then it can just do so without needing any permission.

    If the files in the Documents folder are not created by your app use SAF's ACTION_OPEN_DOCUMENT_TREE to let the user select the Documents folder. Then you can use the tree uri to list the files.