Making a Database Backup to Sdcard on Android

Making a database backup to SDCard on Android

Do you have permissions defined in manifest ?

<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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()).

Backup/Restore to sdcard in Android

I do it like this:

export:

InputStream myInput;
String dbpath = "/data/"+pckgname+"/databases/refuel_db";
String sdpath = Environment.getExternalStorageDirectory().getPath();

try {

myInput = new FileInputStream(Environment.getDataDirectory()
+ dbpath);

// Set the output folder on the Scard
File directory = new File(sdpath + "/Refuel");
// Create the folder if it doesn't exist:
if (!directory.exists()) {
directory.mkdirs();
}
// Set the output file stream up:

OutputStream myOutput = new FileOutputStream(directory.getPath()
+ "/refuel_db");

// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close and clear the streams

myOutput.flush();

myOutput.close();

myInput.close();

Toast.makeText(getActivity(), "Backup Done Succesfully!", Toast.LENGTH_LONG)
.show();

} catch (FileNotFoundException e) {
Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

// TODO Auto-generated catch block
e.printStackTrace();
}

Import:

 OutputStream myOutput;

String dbpath = "/data/"+pckgname+"/databases/refuel_db";
String sdpath = Environment.getExternalStorageDirectory().getPath();

try {

myOutput = new FileOutputStream(Environment.getDataDirectory()
+ dbpath);

// Set the folder on the SDcard
File directory = new File(sdpath + "/Refuel");
// Set the input file stream up:

InputStream myInputs = new FileInputStream(directory.getPath()
+ "/refuel_db");

// Transfer bytes from the input file to the output file
byte[] buffer = new byte[1024];
int length;
while ((length = myInputs.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}

// Close and clear the streams
myOutput.flush();

myOutput.close();

myInputs.close();

Toast.makeText(getActivity(), "Import Done Succesfully!", Toast.LENGTH_LONG)
.show();

} catch (FileNotFoundException e) {
Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getActivity(), "ERROR " + e, Toast.LENGTH_LONG).show();

// TODO Auto-generated catch block
e.printStackTrace();
}

Saving Android Database to SD card?

it is an AsyncTask, so you just have to instantieate a class and call it whenever you want to do the backup:

ExportDatabaseFileTask task = new ExportDatabaseFileTask();
task.execute();

The onPostExecute() will try to show a Toast, so you would need to call the backup from an Activity (or remove those Toasts).

Backup SQLite database to SD card

Try adding runtime permission request for Android 6.0 (API Level 23) and above, from official docs

Code for getting WRITE_EXTERNAL_STORAGE permission

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1 && !checkIfAlreadyhavePermission()) {
requestForSpecificPermission();
}

private boolean checkIfAlreadyhavePermission() {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
return false;
}
return true;
}

Ask for permission else like this

private void requestForSpecificPermission() {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
}

Backup and restore SQLite database to sdcard

Here is my code:

    // Local database
InputStream input = new FileInputStream(from);

// create directory for backup
File dir = new File(DB_BACKUP_PATH);
dir.mkdir();

// Path to the external backup
OutputStream output = new FileOutputStream(to);

// transfer bytes from the Input File to the Output File
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0) {
output.write(buffer, 0, length);
}

output.flush();
output.close();
input.close();

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.



Related Topics



Leave a reply



Submit