I'm using ORMLite and, apart from storing the database in the external public directory, after I restore the file to the database directory, I have to re-instantiate the DatabaseHelper singleton and create a new one.
Here is my version, omitting every try/catch block for the sake of simplicitiy:
public boolean restoreBackup(Context context){
String databasePath = "data/data/my.package.name/databases/myDatabase.sqlite";
String backUpPath = context.getDatabaseDir("myDatabase.sqlite");
// Copies back-up to database directory
new File(databasePath).delete();
FileInputStream streemToBackUp = new FileInputStream(new File(backUpPath));
OutputStream streamToDatabaseFile = new FileOutputStream(databasePath);
byte[] buffer = new byte[1024];
int length;
while ((length = streamToBackUp.read(buffer)) > 0) {
streamToDatabaseFile.write(buffer, 0, length);
}
streamToDatabaseFile.flush();
streamToDatabaseFile.close();
streamToBackUp.close();
// Re-instantiate DatabasHelper singleton
DatabaseHelper.closeHelper();
}
The body of closeHelper() is as follows:
public static void closeHelper() {
helper.close();
}
@Override
public void close() {
super.close();
myDao = null; // Set to null every day you have
helper = null; // Set to null the singleton instance of the helper
}
This will work as long as you don't use OpenHelperManager class to instantiate the helper, and always use getHelper() whenever you need the database instead of storing the instance returned.