How To Restore Sqlite Database After Backup Android
I searched a lot about backup/restore Sqlite database i found code to copy sqlite file to SD card this is the code private void exportDB() { try { File sd = Environment.getExterna
Solution 1:
This is the core code of a working DB restore (from if (dbfile
.. in a try
).
privatestaticfinalintBUFFERSZ=32768;
privatebyte[] buffer = newbyte[BUFFERSZ];
........
dbfile = newFile(currentdbfilename);
.......
if (dbfile.delete()) {
origdeleted = true;
}
FileInputStreambkp=newFileInputStream(backupfilename);
OutputStreamrestore=newFileOutputStream(currentdbfilename);
copylength = 0;
while ((copylength = bkp.read(buffer)) > 0) {
restore.write(buffer, 0, copylength);
}
restore.flush();
restore.close();
restoredone = true;
bkp.close();
The main differences are that I delete the DB file and use writes rather than transfers. Later and upon a successful restore I also use the following to restart the App (might be overkill but it works for me) as you can get unpredictable results (I think parts of the original database may be accessed from memory/cached data):-
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);
System.exit(0);
Post a Comment for "How To Restore Sqlite Database After Backup Android"