我正在尝试将sqlite文件保存到我的SD卡中。我正在使用此question
中的代码 try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.test.mytest\\databases\\test_db";
String backupDBPath = "test_db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, 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) {
}
这是一个非常高评价的答案,所以我认为它应该非常简单。我在logcat中没有错误。我没有看到任何创建的目录/创建的文件。我的清单中也有“写入外部”权限。
答案 0 :(得分:1)
File sd = Environment.getExternalStorageDirectory(); 文件数据= Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/packagename/databases/DATABASENAME";
String backupDBPath = "/backup/"+"main.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, 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();
} else {
Log.e(TAG, "File does not exist: " + currentDBPath);
}
答案 1 :(得分:0)
不要忘记在清单文件中声明权限
uses-permission android:name =“android.permission.WRITE_EXTERNAL_STORAGE”