我试图将我的数据库从内部存储复制到外部,但它不复制数据库。它只是创建一个新的空的sqlite文件。任何想法我实际上错过了什么?android将数据库从内部复制到外部存储返回空数据库
这里是我使用的是什么:
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,但它没有任何的表是空的。我想要做的是将数据库从当前目录移动到新数据库而不丢失任何数据。
任何想法,为什么它不能正常工作?
是你确定扩展名是.sqlite而不是.db? – 2012-01-31 11:46:29
我很确定 – 2012-01-31 11:47:39
通常最好不要假定外部存储称为“/ sdcard” - 总是使用Environment#getExternalStorageDirectory()。另外,不要手动构建数据库文件的路径,请使用Context#getDatabasePath(“stam_sys_tpl.sqlite”)。 – Jens 2012-01-31 12:41:36