I have a database and I want to copy it to SD Card to keep a backup and I found this code:
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.example.myapp" +"/databases/"+"mydatabase";
String backupDBPath = "/storage/extSdCard/mydatabase";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
} catch(IOException e) {
//e.printStackTrace();
Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
}
}
As I wrote in the title I get the No such file or directory
ENOENT error when I try to do it.
Apparently the database should be stored right there. I've tried changing the path to a few different things but still nothing... I tried:
"/data/"+ "com.example.myapp" +"/"+"mydatabase.db"
and
"//data//"+ "com.example.myapp" +"//databases//"+"mydatabase"
Edit1: Also tried this and also doesn't work:
String currentDBPath = this.getDatabasePath("mydatabase").toString();
etc. nothing works and I also can't find a way to check the path in any way.
I basically have the database made, inserted a few rows into it and have some basic functions like add()
and delete()
etc. Am I missing something? I have no idea what else I can try.
Here's what logcat has to say about this:
01-18 11:34:46.215 10337-10337/com.example.myapp D/DBPATH: Database path is /data/data/com.example.myapp/databases/mydatabase.db
01-18 11:34:46.225 10337-10337/com.example.myapp W/System.err: java.io.FileNotFoundException: /data/data/com.example.myapp/databases/mydatabase.db: open failed: ENOENT (No such file or directory)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at java.io.FileInputStream.(FileInputStream.java:76)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at com.example.myapp.TabsActivity.exportDB(TabsActivity.java:183)
01-18 11:34:46.235 10337-10337/com.example.myapp W/System.err: at com.example.myapp.TabsActivity.onOptionsItemSelected(TabsActivity.java:119)