I found a sweetie function to copy my Database File from the original path to External Storage Card:
public static void copyFile(File src, File dst) throws IOException {
if(!dst.exists()) {
dst.createNewFile();
}
else{
dst.delete();
dst.createNewFile();
}
InputStream in = new FileInputStream(src);
try {
OutputStream out = new FileOutputStream(dst);
try {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
But I'm not able to call that function, here my code on a simple button:
exportDatabase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(Info.this, getDatabasePath(Info.this, "Library.sqlite"), Toast.LENGTH_SHORT).show();
File srcFile = new File(getDatabasePath(Info.this, "Library.sqlite"));
File dstFile = new File((getExternalStorageDirectory() + "/Library.sqlite"));
copyFile(srcFile, dstFile);
}
});
This line of code copyFile(srcFile, dstFile);
is marked as error. On mouse over I receive an Unhandled Exeption:java.IO.IOExeption
How can I call it correctly?
Edit: To complete missing code, here the functions used in the button:
public String getDatabasePath(Context context,String databaseName)
{
return context.getDatabasePath(databaseName).getAbsolutePath();
}
public String getExternalStorageDirectory() {
String externalStorageDirectory;
externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
return externalStorageDirectory;
}
I just tested my solution in Memu (android emulator) and that code rocks! I'm now able to Backup and Restore my SQLite database. (to restore simple invert dst and src file paths). Hope this will help. Thanks to all.