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

2
  • 1
    do you want to import an excisting database to your app data and use it later? Commented Aug 29, 2018 at 11:57
  • @fatemehfallahiarezoudar Yes exactly that what I want to do Commented Aug 29, 2018 at 12:12

1 Answer 1

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();
        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

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);
@Mohamad i have eddited my answer see the new dbFilePath.
@Mohamad you should use your own database name instead of DATABASE_NAME
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
Thank you, the code works well, I edited it to fix a small bug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.