在我的应用程序中,我需要备份数据库,但是在我需要再次还原它之后,
我已经读过一些东西,但是我不确定是否有必要拥有根设备,我需要将所有数据备份/还原到非根设备中,这可能吗?
我的第一个想法是创建一个txt文件以写入选择内容,然后再将其插入。
但是我相信这是很多“问题”,然后我不知道是否有可能复制数据库并粘贴到sd卡中进行备份,以及从sd卡复制并粘贴到数据库路径中以用于非根设备还原。
这是一些使其工作的代码
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(); } }