Android:将资产sqlite数据库复制到数据库导向器时出错

时间:2017-03-01 10:14:33

标签: android database file copy fileoutputstream

我想在我的应用中使用预填充数据库,所以我找到了这段代码。

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();
    }

}

" OutputStream os = new FileOutputStream"发现以下错误。


    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

我尝试使用

getDatabasePath(), getDatabasePath().getAbsolutePath()
并直接将这些函数返回的字符串放入
FileOutputStream

我还试图删除" .sqlite"来自数据库名称。

我不明白我做错了什么......

感谢' S

0 个答案:

没有答案