I looked at others answers like Backup and restore SQLite database to sdcard and Restoring SQLite DB file etc. but i still dont see the restoring of database when i uninstall and reinstall app and restore backup. Here is the code I have currently.
public class BackupAndRestore {
public static void importDB(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canRead()) {
File currentDB = context.getDatabasePath(AppDatabase.DATABASE_NAME);
File backupDB = new File(sd, AppDatabase.DATABASE_NAME);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(context, "Database Restored successfully", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void exportDB(Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
File currentDB = context.getDatabasePath(AppDatabase.DATABASE_NAME);
File backupDB = new File(sd, AppDatabase.DATABASE_NAME);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(context, "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
So I install app and I add content to database. Then I also grant permission to write to external storage before calling this export method above. it shows toast message "Backup is successful... " and I can see the file created in the external storage. Then I uninstall and reinstall and I request permission again to external storage. Then call the method import above. The toast message again is seen "Database Restored.." but I don't see the database content that existed before. I tested on android 7 device and android 10 device. I will appreciate help. Thanks.