Search code examples
javaandroidsqlitefragmentsqliteopenhelper

Open Database from Assets...always FileNotFoundException


I have already created an SQLite database. It's up and running... Now, I want to make a backup copy(if possible in the same folder) but after 4 hours I can't. I read a lot of posts regarding this issue...this is my code in DBHelper:

public void copyDataBase(String itinerario, Context c) throws IOException {

        InputStream mInputStream = c.getAssets().open(Constants.DB_NAME);
        String outFileName = DB_PATH+Constants.DB_NAME+itinerario;
        OutputStream mOutputStream = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = mInputStream.read(buffer)) > 0) {
            mOutputStream.write(buffer, 0, length);
        }
        mOutputStream.flush();
        mOutputStream.close();
        mInputStream.close();


}
  • Constants.Name is right (MYDB)
  • If I browse /data/data//databases/ I can find the DB -I tried with explicit path and other solution founded in Internet but without luck!

It always return a FileNotFoundException.

I'm using fragments so I pass Context from Fragment to this method

May be that I make a lame mistake but after 4 hours I don't find errors...the code is quite clear

Can someone help me ? Thanks in advance Alex


Solution

  • You can use Context#getDatabasePath(String). https://developer.android.com/reference/android/content/Context.html#getDatabasePath(java.lang.String)