Function |
Description |
[Database Backup](#database backup) |
Utility function to backup database in sdcard |
/**
* Save database to external storage
*
* @param packageName application package name
* @param databaseName database name
*/
public static boolean backupDatabaseToSdcard(Context context, String packageName, String databaseName) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + packageName + "//databases//" + databaseName;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, databaseName);
Logger.d("current database path: %s %nbackup database path: %s", currentDB, backupDB);
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();
}
Logger.d("database backed up...");
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}