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
Android Studio 4.1 Plugin Error: Plugin * Is Incompatible (Supported Only in Intellij Idea)
Multiple Apps Use Same Content Provider
How to Copy Selected Files from Android with Adb Pull
How to Detect Wifi Tethering State
Android Camera: Onactivityresult() Intent Is Null If It Had Extras
How to Check the Internet Connection Periodically in Whole Application
How to Import a .Aar File into Android Studio 1.1.0 and Use It in My Code
Change Device Language via Adb
Start Android Application Without Activity
Android Can't Record Video with Front Facing Camera, Mediarecorder Start Failed: -19
How to Change the Proguard Mapping File Name in Gradle for Android Project
E/Surface﹕ Getslotfrombufferlocked: Unknown Buffer: 0Xab7519C0