android - Android Java 导入sqlite数据库时没有这样的文件或目录
问题描述
我正在尝试导入一个 sqlite 数据库,以便覆盖当前存在的数据库。我已经设法将数据库导出到手机文件夹中的文件,但是当我尝试导入它时,我得到一个异常,上面写着“没有这样的文件或目录”。我如何解决它?
这是我将数据库导出到文件的代码
private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath= "//data//" + "com.varcon.campus"
+ "//databases//" + "CampusTime.db";
String backupDBPath = "/Download/CampusTime";
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();
}
}
该文件已成功导出到具有其名称的文件夹这是我导入数据库的代码
int PICKFILE_RESULT_CODE = 1;
Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("*/*");
chooseFile = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(chooseFile, PICKFILE_RESULT_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent datas) {
Uri uri = datas.getData();
String filePath = uri.getPath();
try {
//Toast.makeText(this, ""+src, Toast.LENGTH_LONG).show();
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath= "//data//" + "com.varcon.campus"
+ "//databases//" + "CampusTime.db";
String backupDBPath = filePath;
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(), backupDB.toString(),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
//new CustomMessage(getActivity(), "Database replaced sucessfully");
} catch (Exception e) {
e.printStackTrace();
//CustomLog.showLogD("WORKING_STOP",e.getMessage());
}
super.onActivityResult(requestCode, resultCode, datas);
}
而且我的清单中有可用的权限
堆栈跟踪 :
2020-03-10 23:14:43.704 31088-31088/com.varcon.campus D/error: java.io.FileNotFoundException: /storage/emulated/0/Download/CampusTime (No such file or directory)
解决方案
您唯一需要做的就是为选择的 uri 打开一个 InputStream,而不是为一个文件使用 FileInputStream。
InputStream is = getContentResolver().openInputStream(data.getData());
然后像往常一样从流中读取。
推荐阅读
- java - 如何复制列表中的相同项目、更改属性值并将其添加到新列表中?
- javascript - 将 24 小时时间转换为旋转值
- reactjs - 基于产品 ID 的 React fetch API
- report - SQLWatch 报告“不包含数据”
- python - 主函数界面异常处理
- python - 当 div 类名是使用 python 动态时,如何从 div 标签中提取数据?
- c++ - 使用 ROS 或 velodyne 驱动程序从 VLP-16 添加距离场:[x,y,z,distance]
- node.js - 节点问题 - 错误:找不到模块“firebase-functions”
- c - C语言中,给定一个文件目录,如何使用fopen读取文件?
- html - 永久更改 DIV 背景颜色