I am trying to import an sqlite database so that is overrites the one that is currently there. I have managed to export the database to a file in a folder on the phone, but when i try to import it i get the Exception that says " no such file or directory". How do i fix it?
This is my code to export the database to a file
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();
}
}
The file is successfully exported to a folder with its name This is my code to import the database
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);
}
And i have permissions in my manifest available
Stacktrace :
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)