-
Notifications
You must be signed in to change notification settings - Fork 12
Backing up and restoring your database
rutura edited this page Apr 17, 2017
·
1 revision
- Your database is actually a file somewhere in the internal storage of your app on the android system
- Backing up means copying your database file to a location in the external storage so the user for example remove the sd card and store a db backup on her pc or whatever.
- This example contains a bit of database logic code but just focus on storing the existing db file
- We use AsyncTask to handle the backing up and restoring on the background thread *We do the backing up and restoring as shown below:
@Override
protected Integer doInBackground(String... params) {
//Get a reference to the database
File dbFile = mContext.getDatabasePath("foodDb.db");
//Get a reference to the directory location for the backup
File exportDir =
new File(Environment.getExternalStorageDirectory(), "myAppBackups");
Log.d(MainActivity.LOGTAG," Database will be backed up in :"+ exportDir.getAbsolutePath());
if (!exportDir.exists()) {
exportDir.mkdirs();
}
File backup = new File(exportDir, dbFile.getName());
//Check the required operation
String command = params[0];
if(command.equals(COMMAND_BACKUP)) {
//Attempt file copy
Log.d(MainActivity.LOGTAG,"Attempting to copy file :"+dbFile.getAbsolutePath() +
" to "+backup.getAbsolutePath());
if(dbFile.exists())
{
Log.d(MainActivity.LOGTAG,"The source file exits");
}else
{
Log.d(MainActivity.LOGTAG,"The source DOES NOT exits");
}
try {
backup.createNewFile();
fileCopy(dbFile, backup);
return BACKUP_SUCCESS;
} catch (IOException e) {
return BACKUP_ERROR;
}
} else if(command.equals(COMMAND_RESTORE)) {
//Attempt file copy
try {
if(!backup.exists()) {
return RESTORE_NOFILEERROR;
}
dbFile.createNewFile();
fileCopy(backup, dbFile);
return RESTORE_SUCCESS;
} catch (IOException e) {
return BACKUP_ERROR;
}
} else {
return BACKUP_ERROR;
}
}
- You fire up the backup process like this:
Log.d(LOGTAG,"Backing up database");
if( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) {
BackupTask task = new BackupTask(MainActivity.this);
task.setCompletionListener(MainActivity.this);
task.execute(BackupTask.COMMAND_BACKUP);
Log.d(LOGTAG,"Started the backup process.");
}else
{
Log.d(LOGTAG,"Could not find a mountable external storage.");
}
- and the restoring process like this:
if( Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) {
BackupTask task = new BackupTask(MainActivity.this);
task.setCompletionListener(MainActivity.this);
task.execute(BackupTask.COMMAND_RESTORE);
Log.d(LOGTAG,"Started backup restore process.");
}else
{
Log.d(LOGTAG,"Could not find a mountable external storage.");
}
- The AsyncTask informs the caller on the status of its work through the callBack interface methods it calls :
@Override
protected void onPostExecute(Integer result) {
switch(result) {
case BACKUP_SUCCESS:
Log.d(MainActivity.LOGTAG," BackupTask:Backup success");
if(listener != null) {
listener.onBackupComplete();
}
break;
case RESTORE_SUCCESS:
Log.d(MainActivity.LOGTAG," BackupTask:Restore success");
if(listener != null) {
listener.onRestoreComplete();
}
break;
case RESTORE_NOFILEERROR:
Log.d(MainActivity.LOGTAG," BackupTask:Restore_No_File_Error");
if(listener != null) {
listener.onError(RESTORE_NOFILEERROR);
}
break;
default:
Log.d(MainActivity.LOGTAG," BackupTask:Backup Error");
if(listener != null) {
listener.onError(BACKUP_ERROR);
}
}
}
- and the caller Activity implements them to listen in:
public class MainActivity extends AppCompatActivity implements BackupTask.CompletionListener {
...
@Override
public void onBackupComplete() {
Toast.makeText(this, "Backup Complete", Toast.LENGTH_SHORT).show();
}
@Override
public void onRestoreComplete() {
Toast.makeText(this, "Restore Complete", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(int errorCode) {
if(errorCode == BackupTask.RESTORE_NOFILEERROR) {
Toast.makeText(this, "No Backup Found to Restore",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Error During Operation: "+errorCode,
Toast.LENGTH_SHORT).show();
}
}
}
- This callback thing is used all over android.You should be familiar with the mechanics of this example.
- Relevant files:
- Direct link to app: