我正在使用此方法将数据库文件从外部存储复制到我的应用程序的数据目录中。此方法在Android 8.0及更低版本上可以正常使用,但在Android 9上则不能。
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath= "//data//" + "com.mypackage"
+ "//databases//" + "dictionary";
String backupDBPath = "/MyApp/dictionary";
File backupDB = new File(data, currentDBPath);
File currentDB = 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(), "Imported successfully",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
显示“已成功导入”吐司,但未复制文件。该应用程序不会崩溃,logcat上也不会显示任何内容。