I need to make an offline backup of the SQLite database in the mobile's internal storage or the SD Card. But I am clueless, how's this possible.
I followed many threads here on SO, but they suggest copying the DB one location to other, which is not suitable for me, neither to the Google Drive.
Any guidance would be precious.
I used this approach.
public string DATABASE_NAME = "YOUR DATABASE NAME HERE";
public void exportDB(){
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
Log.d("TAG", "DatabaseHandler: can write in sd");
//Replace with YOUR_PACKAGE_NAME and YOUR_DB_NAME
String currentDBPath = "filepath here"+DATABASE_NAME;
//Replace with YOUR_FOLDER_PATH and TARGET_DB_NAME in the SD card
String copieDBPath = DATABASE_NAME;
File currentDB = new File(data, currentDBPath);
File copieDB = new File(sd, copieDBPath);
if (currentDB.exists()) {
Log.d("TAG", "DatabaseHandler: DB exist");
@SuppressWarnings("resource")
FileChannel src = new FileInputStream(currentDB).getChannel();
@SuppressWarnings("resource")
FileChannel dst = new FileOutputStream(copieDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With