I have searched for this but i didn't get an answer. I am working on Budget app and in that there are two type of databases(Sqlite and Shared-preferences). As i can BACKUP/RESTORE of sqlite database in sdcard but I don't know how to backup and restore shared preferences database.
I am able to backup shared-preferences database but don't know how to restore it from sd card.
Databases names:
1.magicbox_database.db is sqlite database,
2.magicbox_database_sf.db is sharedpreferences database
This is a code for Backup for both database.
if (isStoragePermissionGranted() == true) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/data/" + getPackageName() + "/databases/OLBE_DEMO";
String backupDBPath = "magicbox_database.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(sd, backupDBPath);
Toast.makeText(MainActivity.this, "backup creating....", Toast.LENGTH_SHORT).show();
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(MainActivity.this, "Bckup Created !", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "magicbox_database.db in External Storage", Toast.LENGTH_SHORT).show();
}
String currentDBPath1 = "/data/data/" + getPackageName() + "/shared_prefs/DATABASE.xml";
String backupDBPath1 = "magicbox_database_sf.xml";
File currentDB1 = new File(currentDBPath1);
File backupDB1 = new File(sd, backupDBPath1);
// Toast.makeText(MainActivity.this, "backup creating....", Toast.LENGTH_SHORT).show();
if (currentDB1.exists()) {
FileChannel src1 = new FileInputStream(currentDB1).getChannel();
FileChannel dst1 = new FileOutputStream(backupDB1).getChannel();
dst1.transferFrom(src1, 0, src1.size());
src1.close();
dst1.close();
//Toast.makeText(MainActivity.this, "Bckup Created !", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "magicbox_database_sf.db in External Storage", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "ERROR! backup not created", Toast.LENGTH_SHORT).show();
}
}
This code for Restore for both
if (isStoragePermissionGranted() == true) {
try {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/data/" + getPackageName() + "/databases/OLBE_DEMO";
String backupDBPath = "magicbox_database.db";
File currentDB = new File(currentDBPath);
File backupDB = new File(sd, backupDBPath);
Toast.makeText(MainActivity.this, "restoring......", Toast.LENGTH_SHORT).show();
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(MainActivity.this, "Your data Restored !", Toast.LENGTH_SHORT).show();
}
String currentDBPath1 = "/data/data/" + getPackageName() + "/shared_prefs/DATABASE.xml";
String backupDBPath1 = "magicbox_database_sf.xml";
File currentDB1 = new File(currentDBPath1);
File backupDB1 = new File(sd, backupDBPath1);
if (currentDB1.exists()) {
FileChannel src1 = new FileInputStream(backupDB1).getChannel();
FileChannel dst1 = new FileOutputStream(currentDB1).getChannel();
dst1.transferFrom(src1, 0, src1.size());
src1.close();
dst1.close();
Toast.makeText(MainActivity.this, "magicbox_database_sf.db in External Storage", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "ERROR! data not restored", Toast.LENGTH_SHORT).show();
}
}
changes are in this code
FileChannel src1 = new FileInputStream(backupDB1).getChannel();
FileChannel dst1 = new FileOutputStream(currentDB1).getChannel();