我正在尝试从我的应用程序中导出 SQLite 数据库,以便我可以为我的应用程序执行导出/导入功能。
我正在使用此处和此处的示例尝试将SQLite 数据库 (/data/data/com.example.worldcountriesbooks/databases/group.db)从 Android 设备 (Samsung Galaxy Note)、Android 4.0.3 中导出但我不断收到此错误
12-14 00:56:33.722: I/Failed(14850): java.io.FileNotFoundException: /data/data/com.example.worldcountriesbooks/databases/group.db: open failed: ENOENT (No such file or directory)
我也尝试将WRITE_EXTERNAL_STORAGE
&添加WRITE_MEDIA_STORAGE
到清单文件中,但它不起作用。我可以浏览到我的 Android 设备上的实际文件,因为它已植根,但我无法用我的应用程序读取它。知道为什么吗?
示例代码:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//com.example.worldcountriesbooks/databases//group.db";
String backupDBPath = "//mnt//sdcard//database.db";
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(getBaseContext(), backupDB.toString(),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}