我正在尝试将数据库从内部存储复制到外部存储,但它没有复制数据库。它只是创建一个新的空 sqlite 文件。我实际上缺少什么想法吗?
这是我正在使用的:
public static void copyFile(String currentDBPath, String backupDBPath){
try {
File sd = Environment.getExternalStorageDirectory();
//File data = Environment.getDataDirectory();
if (sd.canWrite()) {
File currentDB = new File(currentDBPath);
File backupDB = new File(backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
并像这样使用它:
File database = new File("/sdcard/Stam/Data/");
database.mkdirs();
RPCCommunicator.copyFile("/data/data/" + this.getPackageName()+ "/databases/stam_sys_tpl.sqlite","/sdcard/Stam/Data/system.sqlite");
因此,使用它,它会创建 system.sqlite,但它是空的,没有任何表。我想做的事情是将数据库从当前目录移动到新目录,而不丢失任何数据。
知道为什么它不能正常工作吗?
最佳答案
使用Context.getDataBasePath()
获取您的数据库文件!
关于android将数据库从内部存储复制到外部存储返回空数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9078915/