Copy Database File to Sdcard in Android

copy database file to sdcard in android

Try this hope this helps you

public void exportDatabse(String databaseName) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();

if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "backupname.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();
}
}
} catch (Exception e) {

}
}

How to call

exportDatabse("YourDBName");

NOTE :

Remember to add permission to write to external storage with
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />, otherwise sd.canWrite() will be false.

How do I backup a database file to the SD card on Android?

SQLite databases are completely self-contained files and are portable — you can just copy the entire file straight to the SD card.

Though first I'd check whether an SD card is installed in the device, and what its path is (using Environment.getExternalStorageDirectory()).

Trying to Copy SQLite DB from data to SD card

You are using "MY_DATABASE" literally when you probably actually want to use it as a variable...

Remove the quotes from around it and see if that doesn't solve your problem.

Is it possible to copy database file to SD card?

The database file is just like any other file, if you make a binary file copy it will work.

Java has no built in file copy method, so you can use this:

Standard concise way to copy a file in Java?

Just don't forget to add your manifest permission to write to the SD card:

Permission to write to the SD card



Related Topics



Leave a reply



Submit