我有一种情况,我提示用户是备份他们的消息还是使用Google备份服务。我正在成功地将当前的领域数据库备份到SD卡上,从备份到Google服务。但是,我虽然我想将数据库从SD卡实际恢复到当前领域实例的情况,这意味着将当前领域文件替换为SD卡上可用的文件。
在较旧的版本中,我看到领域提供了一种指定一个自定义路径的方法。
请有任何帮助吗?
创建备份文件
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;
}
希望这有帮助。
信用链接