I am copying database in mobile device's file manager at the time of clicking on Backup button and replacing stored database file with current database file at the time of clicking Restore button.Database is restored in all the versions of android like in marshmallow, nougat, oreo etc but it is not restoring when I am running app in Android Pie. here restoring means I am replacing database file which is stored on device with current database file. So data will be modified as per contained data in the file. This is not happening when I am running app in android pie version. It is working fine in other versions and it is not showing any error also in android pie.
What is the issue please let me know if anyone has solution. Code is provided below for your reference. Thanking you in advance.
@Override // android recommended class to handle permissions
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d("permission", "granted");
} else {
Toast.makeText(BackupAndRestore.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
onDestroy();
}
return;
}
}
}
public void backup(View view) throws IOException {
AlertDialog diaBox = BackupAskOption();
diaBox.show();
}
private AlertDialog BackupAskOption() {
AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this)
.setTitle("Backup")
.setMessage("Are you sure? All changes done till now will be saved.")
.setPositiveButton("Backup", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
int flag = 0;
Toast.makeText(BackupAndRestore.this, filepath, Toast.LENGTH_SHORT).show();
File fileToCopy = new File(filepath);
FileInputStream input = new FileInputStream(fileToCopy);
File newFile = new File(Environment.getExternalStorageDirectory() + "/bandhara_db.db");
FileOutputStream output = new FileOutputStream(newFile);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
flag = 1;
}
input.close();
output.close();
if (flag == 1) {
Intent intent = new Intent(BackupAndRestore.this, WorkerList.class);
startActivity(intent);
Toast.makeText(BackupAndRestore.this, "Backup Completed", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(BackupAndRestore.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myQuittingDialogBox;
}
public void restore(View view) throws IOException {
AlertDialog diaBox = RestoreAskOption();
diaBox.show();
}
private AlertDialog RestoreAskOption() {
AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this)
.setTitle("Restore")
.setMessage("Are you sure? Once you Restore, you will lose all changes done after last backup.")
.setPositiveButton("Restore", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try{
int flag = 0;
String DbFileName = Environment.getExternalStorageDirectory() + "/bandhara_db.db";
File file = new File(DbFileName);
if (!file.exists())
Toast.makeText(BackupAndRestore.this, "Sorry! No Database Found", Toast.LENGTH_SHORT).show();
else {
InputStream mInput = new FileInputStream(Environment.getExternalStorageDirectory() + "/bandhara_db.db");
String outFileName = filepath;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
flag = 1;
}
mOutput.flush();
mOutput.close();
mInput.close();
if (flag == 1) {
Intent intent = new Intent(BackupAndRestore.this, WorkerList.class);
startActivity(intent);
Toast.makeText(BackupAndRestore.this, "Data Restored Successfully", Toast.LENGTH_SHORT).show();
}
}
}
catch (Exception e){
Toast.makeText(BackupAndRestore.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
return myQuittingDialogBox;
}
}