Call the write() method shown below whenever you want your database file. It will create a backupname.db file in the root folder of your device.(you can change the name of the .db file in the backupDBPath string)
private void write() throws IOException {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = DatabaseHelper.DATABASE_NAME;
String backupDBPath = "backupname.db";
File currentDB = new File(getDBPath(), currentDBPath);
File backupDB = new File(sd, backupDBPath);
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();
}
}
}
private String getDBPath() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
} else {
return getFilesDir().getPath() + getPackageName() + "/databases/";
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…