0

I have backed up the sqlite db file to storage using the code below But from Android 11, I have to use mediastore, but I don't know how. Can I back up sqlite db using mediastore?

try {
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    if (sd.canWrite()) {

        String currentDBPath = "//data//" + packageName + "//databases//" + dbName + ".db";
        String backupDBPath = "//" + saveFolder + "//" + dbName + ".db";   // 백업 위치
        File currentDB = new File(data, currentDBPath);
        File backupDB = new File(sd, backupDBPath);

        Log.i("sinwhod","currentDB = " + currentDB);
        Log.i("sinwhod","backupDB = " + backupDB);
        Log.i("sinwhod","backupDBPath = " + backupDBPath);

        FileChannel src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        dst.transferFrom(src, 0, src.size());

        src.close();
        dst.close();
    }


} catch (Exception e) {

}
swiftbegineer
  • 329
  • 1
  • 9
  • "But from Android 11, I have to use mediastore" -- or you could use the Storage Access Framework. [This sample app](https://gitlab.com/commonsguy/cw-room/-/tree/v0.5/ImportExport) demonstrates that, in the context of Kotlin and Room. Also, *please* use `getDatabasePath()` for getting the location of your database, not your current code for `currentDBPath`. – CommonsWare Apr 16 '21 at 13:09
  • https://stackoverflow.com/questions/59511147/create-copy-file-in-android-q-using-mediastore – Sam Chen Apr 16 '21 at 14:38

0 Answers0