How to get the BackUp data into the SD Card & Restore it again into the app?
// Method to get Backup from an app Databaseprivate void exportDB() {try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "App Package Name" + "//databases//" + DATABASES[0];
String backupDBPath = DATABASES[0];
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
// Method to restore data into an app
marysudha[6:45 PM]private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String backupDBPath = "//data//" + "com.android.e_commerceapp" + "//databases//" + DATABASES[0];
String currentDBPath = DATABASES[0];
File currentDB = new File(sd, currentDBPath);
File backupDB = new File(data, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
//Database Name//Here we can use multiple database(.db)
private static final String[] DATABASES = {
"onlineshopping.db", // DATABASE[0]-- It is 0th array element.
};
- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
Comments
Post a Comment