How to put .db file from assets to data/data/packagename/ android [duplicate]

喜夏-厌秋 提交于 2019-12-14 00:26:41

问题


Possible Duplicate:
How to use an existing database with an Android application

how to put .db file from assets to data/data/packagename/ without using copying content of .db file in assets. I don't want to create database because its useless to put the .db file in assets. I explore on it but all are again creating databse but i only want to put that .db file directly.


回答1:


use this

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


来源:https://stackoverflow.com/questions/14379433/how-to-put-db-file-from-assets-to-data-data-packagename-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!