Currently I am working to implement a SQlite db backup feature in my application with stream. when I backup the database it works and the file has data as I have verified it through "Db browser for SQlite". But when try to restore through FilOuputStream from the database path of my application and InputStream from returned UrI by android SAF(Storage access framework) pointing to external storage where I have put db backup, I get corrupted database file and the connection is also closed.
following are the two methods I am using for this purpose
Taking Backup
//back db to a URI
public synchronized static boolean backupDb(Context context, Uri uri, String dbNam) throws IOException {
File dbFile = new File(context.getDatabasePath(dbNam).getPath());
FileInputStream inFilStream = new FileInputStream(dbFile);
OutputStream outFilStream = context.getContentResolver().openOutputStream(uri);//new FileOutputStream(backupFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inFilStream.read(buffer)) > 0) {
outFilStream.write(buffer, 0, length);
}
outFilStream.flush();
outFilStream.close();
inFilStream.close();
return true;
}
Restoring Backup
//restore db from a URI
public static synchronized boolean restoreBackup(Context context, Uri uri, String dbNam) {
try {
InputStream inFilStream = context.getContentResolver().openInputStream(uri);
File dbFile = new File(context.getDatabasePath(dbNam).getPath());
FileOutputStream outFilStream = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inFilStream.read(buffer)) > 0) {
outFilStream.write(buffer, 0, length);
Log.wtf("db", "restoring backup up");
}
outFilStream.flush();
// using outFilStream.getFD().sync(); also not working
outFilStream.close();
inFilStream.close();
return true;
} catch (IOException e) {
return false;
}
}
Log
I don't Understand why it is doing so as it is quite weird when I debug and put break points in the restore method it works, could any please help to figure out what is wrong there.