I'm working on an Android app that uses a SQLite database for storing survey data.
Problem: For Android version 10 and above devices,I can't store a backup copy of an SQLite database in some folder that won't be deleted even if app is uninstalled
For devices before Android version 10 it works fine with code below:
folder = Environment.getExternalStoragePublicDirectory("Db_Backup");// Folder Name
if (!folder.exists())
folder.mkdirs(); //Db_Backup folder gets created
For devices with Android version 11,it is not working
For android 10 and 11 devices, I have tried using code as below:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Files[] files=getExternalFilesDirs(Environment.DIRECTORY_DOWNLOADS);
folder=files[0];
// OR
folder = getFilesDir();
if (!folder.exists())
folder.mkdirs(); // Db_Backup folder is created in app directory
......
}
Above code creates a backup copy to android/data/com.example.packagename/files/backup/{filename.db} this location only
Issue: when app is uninstalled or app data gets cleared the .db backup file also gets removed or cleared
exportDatabaseNew() function that I am using is as below:
private void exportDatabaseNew(LinearLayout linearLayout) {
FileOutputStream output = null;
try {
File dbFile = getDatabasePath(DatabaseHelper.DATABASE_NAME);
FileInputStream fis = new FileInputStream(dbFile);
File[] files;
File folder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
files=getExternalFilesDirs("Db_Backup");
folder=files[0];
Log.i("export", "exportDatabaseNew: " folder.getAbsolutePath());
} else {
folder = Environment.getExternalStoragePublicDirectory("Db_Backup");//Folder Name
Log.i("export", "exportDatabaseNew: " folder.getAbsolutePath());
}
if (!folder.exists())
folder.mkdirs();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
String currentDate = df.format(new Date());
String backupName = "Survey_" currentDate ".db";
File myFile = new File(folder, backupName);// Filename
output = new FileOutputStream(myFile);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Snackbar.make(linearLayout, R.string.backup_complete, Snackbar.LENGTH_LONG)
.show();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
CodePudding user response:
For Android 11, you have to either:
- Save files in your application directory
- Save files into public directories
- If you really must, any other location that is NOT a different app directory, it requires the following permission:
< uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>
This permission only applies for android 11 and above. Do note that if you want your app to be published on Google Play Store you'll need to provide a good reason why you want this permission and you are not willing to simply store it as part of your app's files.
CodePudding user response:
Create your own folder in public Documents directory and write your file to that folder.