1

I want to use a pre fill database in my app so I found this code.

DB_NAME = "urg1718.sqlite"

public SQLiteDatabase openDatabase() {
    File dbFile = context.getDatabasePath(DB_NAME);
    String path = dbFile.getAbsolutePath();
    Log.i("path", "openDatabase: " + path);

    if (!dbFile.exists()) {
        try {
            //the file doesn't exist yet so this function is called
            copyDatabase(dbFile);
        } catch (IOException e) {
            throw new RuntimeException("Error creating source database", e);
        }
    }
    //check if the database is present in the folder
    Log.i("database", "openDatabase: " + dbFile.exists());

    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}

private void copyDatabase(File dbFile) throws IOException {
    //pre fil database
    InputStream is = context.getAssets().open(DB_NAME);
    try {
        //where the error occurs
        OutputStream os = new FileOutputStream(dbFile);

        byte[] buffer = new byte[1024];
        while (is.read(buffer) > 0) {
            os.write(buffer);
        }

        os.flush();
        os.close();
        is.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();;
    } catch (IOException e) {
        e.printStackTrace();
    }

}

The "OutputStream os = new FileOutputStream" catch the following error.


    java.io.FileNotFoundException: /data/data/jle.urgdegarde17_18/databases/urg1718: open failed: ENOENT (No such file or directory)
    W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
    W/System.err:     at java.io.FileOutputStream.(FileOutputStream.java:87)
    W/System.err:     at java.io.FileOutputStream.(FileOutputStream.java:127)
    W/System.err:     at java.io.FileOutputStream.(FileOutputStream.java:116)
    W/System.err:     at jle.urgdegarde17_18.AssetDatabaseOpenHelper.copyDatabase(AssetDatabaseOpenHelper.java:49)
    W/System.err:     at jle.urgdegarde17_18.AssetDatabaseOpenHelper.openDatabase(AssetDatabaseOpenHelper.java:35)
    W/System.err:     at jle.urgdegarde17_18.Chargement.isStoragePermissionGranted(Chargement.java:40)
    W/System.err:     at jle.urgdegarde17_18.Chargement.onCreate(Chargement.java:18)
    W/System.err:     at android.app.Activity.performCreate(Activity.java:5990)
    W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:151)
    W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    W/System.err:     at android.os.Looper.loop(Looper.java:135)
    W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
    W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    W/System.err:     at libcore.io.Posix.open(Native Method)
    W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)
    W/System.err:   ... 20 more

I tried to use

getDatabasePath(), getDatabasePath().getAbsolutePath()
and directly put the string returned by those functions into
FileOutputStream

I also tried to remove the ".sqlite" from the database name.

I don't understand what i'm doing wrong ...

Thank's.

0 Answers0