0

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?

B-DK
  • 35
  • 6
  • Why do you need that permission to backup the db? The db is your file in your space, you don't need any permission to access it. – Gabe Sechan Nov 18 '21 at 06:28
  • Perhaps you can give the option to share the database? Use the Android Sharesheet and allow your users to export the file externally. – Filip Petrovski Nov 18 '21 at 07:03
  • @FilipPetrovski so if they user does the export/import themselves permissions aren't needed? – B-DK Nov 18 '21 at 09:59

3 Answers3

0

Google is restricting use of broad file permissions such as MANAGE_EXTERNAL_STORAGE. You can use Storage Access Framework to gain limited access to certain files or directories.

// Request code for selecting a PDF document.
const val PICK_PDF_FILE = 2

fun openFile(pickerInitialUri: Uri) {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "application/pdf"

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

    startActivityForResult(intent, PICK_PDF_FILE)
}

Or if you want to access an entire directory;

fun openDirectory(pickerInitialUri: Uri) {
    // Choose a directory using the system's file picker.
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when it loads.
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(intent, your-request-code)
}

There are some restrictions to which paths you can access. You can read more about it here https://developer.android.com/training/data-storage/shared/documents-files

Shreyash.K
  • 487
  • 4
  • 14
0

You can just backup your db file to the public Documents directory.

No need for the permissions you mentioned.

blackapps
  • 8,011
  • 2
  • 11
  • 25
0

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

B-DK
  • 35
  • 6