is it possible backup and RESTORE a database file in android? non root devices

21,494

Here is some code to make it work

private void importDB() {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
                if (sd.canWrite()) {
                String currentDBPath = "//data//" + "<package name>"
                        + "//databases//" + "<database name>";
                String backupDBPath = "<backup db filename>"; // From SD directory.
                File backupDB = new File(data, currentDBPath);
                File currentDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(backupDB).getChannel();
            FileChannel dst = new FileOutputStream(currentDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Import Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}

private void exportDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + "<package name>"
                    + "//databases//" + "<db name>";
            String backupDBPath = "<destination>";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Backup Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}
Share:
21,494
angel
Author by

angel

Updated on March 17, 2020

Comments

  • angel
    angel about 4 years

    in my app I need get a backup of my database, but after I'll need restore it again,

    i have read somethings, but i do not sure if this is necessary to have a rooted device, i need backup/restore the all data in non root devices, is it possible?

    my first idea was creating a txt file for write the select, and later insert it again.

    but i believe this is much "problem" then i don't know if this is possible copy the database and paste in sd card for backup, and copy from sd card and paste in path of database for restore for non root devices.

  • angel
    angel over 10 years
    yes, this worked for non-root devices :)
  • Thorben
    Thorben almost 10 years
    You could extract a method "copyDB" or something else instead of using the same code in both methods. This would increase modularity. In addition to that closing all opened Streams in finally blocks would be fine =)
  • Thorben
    Thorben almost 10 years
    Besides, getting the current db path via getDatabasePath("<database name>") should be more robust.
  • Shabbir Dhangot
    Shabbir Dhangot almost 10 years
    Not Working getting FileNotFoundException
  • karan vs
    karan vs over 7 years
    I followed this, but getting "No such file or directory" while importing database, I get this while initializing the FileChannel for currentDBPath
  • Ahesanali Suthar
    Ahesanali Suthar about 7 years
    In my case export/import successful but database showing older values not new one from imported database.