1

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;
}

}

  • Please [edit] your question to explain exactly how it is not working. – Mike M. Aug 07 '19 at 07:35
  • Database is restored in all 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. @MikeM. – Aliasgar Aabadani Aug 07 '19 at 10:13
  • Please [edit] your question to add those details. Also, have a look at [this post](https://stackoverflow.com/q/50476782) to see if any of the issues described in the answers there might apply to your setup. – Mike M. Aug 07 '19 at 10:17
  • 1
    Thank you so much.. one of the solution worked for me from given link. @MikeM. – Aliasgar Aabadani Aug 07 '19 at 14:40
  • App icon is also not showing in Android pie, It is showing in other versions. Please let me know if do you have any solution @MikeM. – Aliasgar Aabadani Aug 07 '19 at 14:43
  • Sorry, I'm not sure what the icon issue could be. Please feel free to post a new question for that. Glad you got this one fixed, though. Cheers! – Mike M. Aug 07 '19 at 14:51

0 Answers0