I have this application for years and after updating the application I found that I could no longer restore the data. I'm able to backup and I can see all data in DB Browser for SQLite on my desktop. When I install the app on a new device, i copy the backup to the same folder and do a restore, no data shows. I get no errors when I restore and I see the data get restored from the right folder when I run in debugging mode in Android Studio. Haven't changed anything in backup and restore function.
The Android Studio Chipmunk 2021.2.1 Patch 1 Target SDK 34 Min SDK 28
I tried different Target and Min, but with no luck.
private void restoredata(){
InputStream myInput;
String dbpath = "/data/com.example.myapp/databases/MyData.db";
String sdpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
Boolean FileErr=true;
try {
File exist = new File(Environment.getDataDirectory() + dbpath);
System.out.println("1 " + exist.exists());
// Set the output folder on the Scard
File directory = new File(sdpath + "/MyBack");
File file = new File(sdpath + "/MyBack/MyBackup.db");
// Create the folder if it doesn't exist:
if (!directory.exists() || !directory.isDirectory()) {
showMyAlert("Folder Download/MyBack not found!");
FileErr = false;
} else if (!file.exists()) {
showMyAlert("File MyBackup.db not found!");
FileErr = false;
}
if (FileErr) {
// Set the input file stream up:
try {
myInput = new FileInputStream(directory.getPath() + "/MyBackup.db");
OutputStream myOutput = new FileOutputStream(Environment.getDataDirectory() + dbpath);
// 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(SetupPage.this, "Restore Done Succesfully!", Toast.LENGTH_LONG)
.show();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}