1
사용자가 응용 프로그램을 삭제하고 다시 설치하면 데이터를 복구 할 수 있도록 응용 프로그램에서 데이터베이스에 대한 백업 및 복원을 수행하려고합니다. Android Studio에서 가장 좋은 방법은 무엇입니까?백업 및 복원 데이터베이스 android studio
사용자가 응용 프로그램을 삭제하고 다시 설치하면 데이터를 복구 할 수 있도록 응용 프로그램에서 데이터베이스에 대한 백업 및 복원을 수행하려고합니다. Android Studio에서 가장 좋은 방법은 무엇입니까?백업 및 복원 데이터베이스 android studio
Google 드라이브, 드롭 상자 및 드라이브와 같은 db 파일의 백업 및 복원에 사용할 수있는 몇 가지 유형이 있습니다. 로컬 저장소에서 백업을 수행하려면 아래 주어진 코드를 시도하십시오.
백업 코드 :
public void backUp() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//your package name//databases//dbname.db";
String backupDBPath = "dbname.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
Log.d("backupDB path", "" + backupDB.getAbsolutePath());
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 e) {
e.printStackTrace();
}
}
복원 코드 :
public void restore() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//your package name//databases//dbname.db";;
String backupDBPath = "dbname.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
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(getApplicationContext(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}