Как сделать резервную копию базы данных на внешнем хранилище с помощью DBFlow
- как сделать резервную копию базы данных приложения и хранить в мобильном хранилище
папка. Я пытаюсь сделать это, но не могу создать резервную копию файла.
try{
File data = Environment.getDataDirectory();
File sd = new File(Environment.getExternalStorageDirectory()+"/digiplusBackUp");
if(!sd.exists()){
sd.mkdirs();
}
Calendar c = Calendar.getInstance();
SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate1 = df1.format(c.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String formattedDate3 = dateFormat.format(c.getTime());
if (sd.canWrite()) {
String currentDBPath = "//data//com.digiplus.live//databases//digiplus";
String backupDBPath = "DigiPlus"+formattedDate1+formattedDate3 ;
File currentDB = new File(data, 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();
Toast.makeText(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception ignored) {
Log.d("Error",ignored.tostring());
}
}
1 ответ
этот код решит мою проблему спасибо MikeT за вашу поддержку
try {
File data = Environment.getDataDirectory();
File sd = new File(Environment.getExternalStorageDirectory() + "/digiplusBackUp");
if (!sd.exists()) {
sd.mkdirs();
}
Calendar c = Calendar.getInstance();
SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate1 = df1.format(c.getTime());
SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");
String formattedDate3 = dateFormat.format(c.getTime());
if (sd.canWrite()) {
String currentDBPath = "/data/com.digiplus.lve/databases/digiplus.db";
String backupDBPath = "backupFolder" + formattedDate1 + formattedDate3;
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(getApplicationContext(), "Backup is successful to SD card", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.d("Error", e.toString());
}
}