skip to Main Content

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?

3

Answers


  1. Chosen as BEST 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


  2. 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

    Login or Signup to reply.
  3. You can just backup your db file to the public Documents directory.

    No need for the permissions you mentioned.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search