[英]Restore Realm DB from SD Card/External Storage Android
我遇到一種情況,我會提示用戶是否使用Google備份服務備份他們的郵件還是不備份。 我已成功將當前領域數據庫備份到SD卡上,將從那里進行Google服務備份。 但是,盡管如此,我還是想將數據庫從SD卡實際還原到當前的Realm Instance,這意味着用SD卡上可用的文件替換當前的Realm文件。
在較舊的版本中,我看到Realm提供了一種指定自定義路徑的方式,Realm從該路徑中讀取數據庫文件,但是在這個新版本中,我什么都看不到。
有什么幫助嗎?
創建備份文件
public void backup() {
try {
// create a backup file
File exportRealmFile;
exportRealmFile = new File(EXPORT_REALM_PATH, EXPORT_REALM_FILE_NAME);
// if backup file already exists, delete it
exportRealmFile.delete();
// copy current realm to backup file
realm.writeCopyTo(exportRealmFile);
} catch (IOException e) {
e.printStackTrace();
}
realm.close();
}
恢復
private String copyBundledRealmFile(String oldFilePath, String outFileName) {
try {
File file = new File(activity.getApplicationContext().getFilesDir(), outFileName);
FileOutputStream outputStream = new FileOutputStream(file);
FileInputStream inputStream = new FileInputStream(new File(oldFilePath));
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
outputStream.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
希望能有所幫助。
歸功於鏈接
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.