Skip to content
Advertisement

Concerns about the FILES AND MEDIA PERMISSIONS on Android as a developer

I’m developing an app that saves data into a database, I’m trying to backup and restore that database which I am able to do, my issue is with the “ominous” permmission popup on API30+

Allow management of all files

Allow this app to access modify and delete files on your device…..

Allow this app to access, modify and delete files on the device or any connected storage devices? this app may access files without asking you.

I’m not trying to do any of these things, I just want permission to do the backup/restore thing

here’s my code for requesting permission:

    private void requestStoragePermissionExport(){
        if( (Build.VERSION.SDK_INT  >= 30 )){
            try {
                Intent intent = new Intent(Manifest.permission.MANAGE_EXTERNAL_STORAGE);
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse(String.format("package:%s",getApplicationContext().getPackageName())));
                startActivityForResult(intent, 2296);
            } catch (Exception e) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivityForResult(intent, 2296);
            }
        }else{
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, BACKUP_CODE);
        }
    }

is there a better way to handle this?

Advertisement

Answer

Alright so, after a bit of research I found the best solution for myself is as follows:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + File.separator + "foldername"

this doesn’t require permissions and works on below and above API 30

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement