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 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
Alright so, after a bit of research I found the best solution for myself is as follows:
this doesn't require permissions and works on below and above API 30
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.Or if you want to access an entire directory;
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
You can just backup your db file to the public Documents directory.
No need for the permissions you mentioned.