One of our app requirements is that some SQLite file should be retained and backup, even the app is uninstalled.
We achieve such, by writing SQLite file via Room, into location Environment.getExternalStorageDirectory
RoomDatabase with customizable SQLite file path
@Database(
entities = {Backup.class},
version = 1
)
public abstract class LocalBackupNamedRoomDatabase extends RoomDatabase {
public abstract BackupDao backupDao();
public static LocalBackupNamedRoomDatabase newInstance(String dbName) {
LocalBackupNamedRoomDatabase INSTANCE = Room.databaseBuilder(
WeNoteApplication.instance(),
LocalBackupNamedRoomDatabase.class,
dbName
)
.build();
return INSTANCE;
}
}
Function returns Environment.getExternalStorageDirectory
public static String getBackupDirectory() {
if (backupDirectory == null) {
File _externalStorageDirectory = Environment.getExternalStorageDirectory();
if (_externalStorageDirectory == null) {
return null;
}
try {
backupDirectory = _externalStorageDirectory.getCanonicalPath();
} catch (IOException e) {
Log.e(TAG, "", e);
}
if (backupDirectory == null) {
return null;
}
backupDirectory = toEndWithFileSeperator(backupDirectory) + "com.yocto.wenote" + File.separator + "backup" + File.separator;
}
return backupDirectory;
}
How we save SQLite file via Room in external storage
LocalBackupNamedRoomDatabase.newInstance(
getBackupDirectory() + "local-backup"
).backupDao().insert(backup)
According to this, my understanding is that Environment.getExternalStorageDirectory
might not be usable anymore (am I correct??).
My guess is that, in order to achieve the same behavior, I might need to use MediaStore.Downloads
? But how?
Can anyone provide a concrete example for the above use case? Or, we are no longer possible to achieve such behavior, under Scoped storage?