lundi 15 juin 2015

android copy database from assets folder

I having trouble coping a database from the assets folder to the databases folder. When the user start the application I check if the database doesnt exists and if true I copy the database.

Where is my code:

    private void CopyDatabaseIfNotExists() {

        dbName = "quizdb.db";

        File f = getDatabasePath(dbName);
        if (f.exists())
            return;

        System.out.println("db missing");

        try {

            InputStream mInputStream = getAssets().open(dbName);

            OutputStream mOutputStream = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = mInputStream.read(buffer)) > 0) {
                mOutputStream.write(buffer, 0, length);
            }
            mOutputStream.flush();
            mOutputStream.close();
            mInputStream.close();

        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

And I got this error:

06-15 12:29:04.882  25037-25037/com.ex.example W/System.err﹕ java.io.FileNotFoundException: /data/data/com.ex.example/databases/quizdb.db: open failed: ENOENT (No such file or directory)

I already tried search for the soluction but cant find. Somebody can help me? Thanks and sorry my english.

Aucun commentaire:

Enregistrer un commentaire