是否有可能在android中备份和恢复数据库文件? 非根设备

在我的应用程序,我需要得到我的数据库的备份,但之后我需要再次恢复,

我已经读了一些,但我不知道如果这是必要的,有一个根植的设备,我需要备份/恢复非根设备中的所有数据,这可能吗?

我的第一个想法是创build一个txt文件写入select,然后再次插入它。

但我相信这是很多“问题”,那么我不知道这是可能的复制数据库并粘贴在SD卡备份,从SD卡复制并粘贴到非根设备恢复数据库的path。

Solutions Collecting From Web of "是否有可能在android中备份和恢复数据库文件? 非根设备"

这是一些代码,使其工作

private void importDB() { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + "<package name>" + "//databases//" + "<database name>"; String backupDBPath = "<backup db filename>"; // From SD directory. File backupDB = new File(data, currentDBPath); File currentDB = new File(sd, backupDBPath); 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(), "Import Successful!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT) .show(); } } private void exportDB() { try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { String currentDBPath = "//data//" + "<package name>" + "//databases//" + "<db name>"; String backupDBPath = "<destination>"; 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 Successful!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT) .show(); } }