I’m trying to export my application’s database to SDcard. It works excellent for emulator, bur not for my phone.
OnClickListener mExportListener = new OnClickListener(){
public void onClick(View v){
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.mypck.myapp\\databases\\database";
String backupDBPath = "database.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}else{
String msg = activity.getResources().getString(R.string.something_wrong);
Toast toast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
toast.show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
This currentDB.exists() returns false on the phone, but I checked the file – it exists.
What is wrong with my phone?
You are supplying
Filewrong parameters. From Android doc:In your code:
It could be like this:
It surprises me this works on emulator, I doubt that. It probably creates an empty file becuase at least this line makes some sort of sense, although your variables are not what you named them:
Also you should probably create a
finallyblock and do someFileChannelclean up in there.