在我的情况下,我会提示用户是否使用google备份服务备份他们的消息。我正在成功地将当前的领域数据库备份到SD卡上,从那里备份到google服务。但是,我仍然想要将数据库从SD卡恢复到当前领域实例,这意味着用SD卡上可用的文件替换当前的领域文件。
在较早的版本中,我看到这个领域提供了一种方法来指定一个自定义路径,该路径将从哪个领域读取数据库文件,但在这个新版本中,我没有看到任何一个。
有什么帮助吗?
发布于 2017-02-17 08:20:20
创建备份文件
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;
}希望能帮上忙。
贷给链接
https://stackoverflow.com/questions/42292707
复制相似问题