0

I am trying to restore a database from the phone storage in order to use it in my application. I successfully did a backup for my database to the internal storage but when I am trying to restore it the following io.exception pops up

java.io.IOException: open failed: ENOENT (No such file or directory)

How can I solve this problem? However, I tried the follosing existing solutions but they didn't work Android - java.io.FileNotFoundException, is it possible backup and RESTORE a database file in android? non root devices, Restoring SQLite DB file

private void restoreDatabase(Context context) {
    String packagename = getPackageName();

    File sdcard = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();

    try {
        if (sdcard.canWrite()) {
            String currentDBPath = "//data/"+getPackageName() +"/databases/" + DATABASE_NAME;
            String backupDBPath = DATABASE_NAME;
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sdcard, backupDBPath);
            currentDB.createNewFile();

            if (currentDB.exists()) {
                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(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
    }
}

Please don't mark this question as duplicate

Milind Mevada
  • 3,145
  • 1
  • 14
  • 22
Ahmad
  • 397
  • 2
  • 7
  • 18

1 Answers1

3

after exporting your database you can use this function to import the database, I had it testet and it works fine :

private void importDB() {
        String appDataPath = getApplicationContext().getApplicationInfo().dataDir;

        File dbFolder = new File(appDataPath + "/databases");//Make sure the /databases folder exists
        dbFolder.mkdir();//This can be called multiple times.
        //EDITED
        File dbFilePath = new File(appDataPath + "/databases/"+"yourDataBaseName");

        try {
        //EDITED
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "YourBackupFolder","yourDataBaseFileName);
        FileInputStream inputStream = new FileInputStream(file); //use your database name
            OutputStream outputStream = new FileOutputStream(dbFilePath);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer))>0)
            {
                outputStream.write(buffer, 0, length);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();

        } catch (IOException e){
            //handle

            e.printStackTrace();
        }
    }
  • It didnt work for me. My database exists on the phone storage and I want to import it into my application. Your code gives me a IO exception on InputStream inputStream = getApplicationContext().getAssets().open(DATABASE_NAME); – Ahmad Aug 29 '18 at 12:17
  • @Mohamad i have eddited my answer see the new dbFilePath. – fatemeh fallahi arezoudar Aug 29 '18 at 12:24
  • @Mohamad you should use your own database name instead of DATABASE_NAME – fatemeh fallahi arezoudar Aug 29 '18 at 12:27
  • I am storing the database name in DATABASE_NAME. I think you reversed the streams, the input stream should point to the db on external storage, and the output stream should be the db file in the application – Ahmad Aug 29 '18 at 12:31
  • Thank you, the code works well, I edited it to fix a small bug – Ahmad Aug 29 '18 at 14:01