(无此类文件或目录)在Android上将SQLite DB从外部存储导入内部存储时出错
我尝试为我的用户提供导入和导出数据库的选项。 每当我尝试导入文件时,都会出现以下错误: E/tag:/storage\u root/APPNAME/MYDB(没有这样的文件或目录) 目录似乎是正确的,但不知何故,我无法打开/复制该文件。我已经尝试了很多不同的选择。 所有这些代码都在一个片段中 代码: // //(无此类文件或目录)在Android上将SQLite DB从外部存储导入内部存储时出错,android,mysql,Android,Mysql,我尝试为我的用户提供导入和导出数据库的选项。 每当我尝试导入文件时,都会出现以下错误: E/tag:/storage\u root/APPNAME/MYDB(没有这样的文件或目录) 目录似乎是正确的,但不知何故,我无法打开/复制该文件。我已经尝试了很多不同的选择。 所有这些代码都在一个片段中 代码: // // 非常感谢您的帮助:)只有当DB目录的路径不正确时,才会出现此错误。尝试将绝对路径(全部使用静态文件夹名称)放入代码中 谢谢你,马杜尔。这确实很有帮助,但取决于文件的选择方式,我得到了很多
非常感谢您的帮助:)只有当DB目录的路径不正确时,才会出现此错误。尝试将绝对路径(全部使用静态文件夹名称)放入代码中 谢谢你,马杜尔。这确实很有帮助,但取决于文件的选择方式,我得到了很多不同的路径,它们都不是绝对路径。比如:content://com.amaze.filemanager.FILE_PROVIDER/storage_root/DOWNLOADS content://com.android.providers.downloads content://com.google.android.apps.docs.storage.legacy/enc%3DRzI4Dwk1Qa7...
importSQL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, GET_DB);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("OnActivityResultImport", "Bitmap was selected " + requestCode + " " + resultCode);
if (resultCode == Activity.RESULT_OK){
switch (requestCode) {
case GET_DB:
Uri selectedImage = data.getData();
//Find DB to import
String PathHolder = data.getData().getPath();
importDB(PathHolder);
break;
}
}
}
public void importDB(String path){
if(isExternalStorageWritable()) {
//check if it really is a valid SQL DB file
if(isValidSQLite(path)) {
String outputPath = exportSQL.getContext().getFilesDir().getAbsolutePath().split("files")[0] + "databases/";
Log.d("WritingDB", "storage is writeable\nReading DB from: " + path);
String outputFile = "MYDB";
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File(outputPath);
if (!dir.exists()) {
dir.mkdirs();
}
in = new FileInputStream(path);
out = new FileOutputStream(outputPath + outputFile);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file
out.flush();
out.close();
out = null;
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Toast.makeText(getContext(), R.string.exportSuccessMessage, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getContext(), "Thats not a valid DB!", Toast.LENGTH_LONG).show();
}
}
}