I have written code to back up my sqlite db
to a file
on my Android phone (Sony Ericsson X-8 w/ version 2.1.1 of Android).
I have put the code below for reference, but before reading it, I stress that it works fine on the X-8. [Any suggestions for improvement are still most welcome!] It is when I run it on a Sony Ericsson Xperia Arc w/ version 4.0.4 of Android that I have problems.
The file is not written to the SD card
. It's very hard to debug, because as you may know, hooking up the phone to the computer via USB card can stop the file from being written anyhow (see for example: Android FileWriter not working on Sony Ericsson Arc but works on another phone).
In any case, going through the code in the debugger results in no problems and one is led to believe the file is written. In my manifest
, I do have:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I am stumped as to what is going on. Did something change between versions 2 and 4 of Android in regards to the file system? Is it phone specific?
Any ideas would be much appreciated. -Dave
String packageName = "com.xxx.receiver";
String dbName = "Receiver";
//File sd = Environment.getExternalStorageDirectory();
File sd = new File(Environment.getExternalStorageDirectory(), "xxx"); // "xxx" is never created on Arc!
if (!sd.exists()) {
sd.mkdirs();
}
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
String currentDBPath = "//data//"+ packageName +"//databases//"+ dbName;
String backupDBPath = dbName;
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(SettingsAdapter.this.getContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
}