I am a newbie in android programming and looking for your help. I am working on creating an android application that saves data into Sqlite database.. I managed that and could view the data from the emulator after I pulled it out. Now I am seeking a way to copy this internal sqlite to SD card pro grammatically.
I searched the solution and I found this :
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//{package name}//databases//{database name}";
String backupDBPath = "{database name}";
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();
}
}
} catch (Exception e) {
}
However, I do not know how to integrate this piece of code to my entire code. Should I write it at each time I insert data ? I would be so grateful for any help I may get.. Any further explanations or other suggestions would be appreciated.