如何将.db文件从资源放到data/data/packagename/android

Use*_*590 2 android android-sqlite

可能重复:
如何将现有数据库与Android应用程序一起使用

如何将.db文件从资产放入data/data/packagename /而不使用资产中的.db文件复制内容.我不想创建数据库,因为将.db文件放在资产中是没用的.我探索它,但所有再次创建数据库但我只想直接放置.db文件.

San*_*ela 8

用这个

private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    // Path to the just created empty db
    String outFileName =  "/data/data/"
            +getApplicationContext().getPackageName()
            + "/databases/" + DB_NAME;
    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);

    }
    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
Run Code Online (Sandbox Code Playgroud)

  • 我不想用它.我只想在data/data/packagename中复制.db文件 (2认同)