3

'm using following code to copy sqlite file from asset folder to database folder. i found this example here find CommonsWare's answer in this question
But im getting java.io.FileNotFoundException: /file:/android_asset/pg558.sqlite (No such file or directory)

void copy() throws IOException {

    InputStream in =getApplicationContext().getAssets().open("pg558.sqlite");
    OutputStream out = new FileOutputStream("data/data/com.mireader/databases/MIBOOK");

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}
Community
  • 1
  • 1
vnshetty
  • 20,051
  • 23
  • 64
  • 102

2 Answers2

2

Please check you pg558.sqlite file, I've tried you code and it worked for my file. I copied a xml file from assets/ to /mnt/sdcard/.

herbertD
  • 10,657
  • 13
  • 50
  • 77
0

To copy a file from assets folder to /databases folder:

public static final String DATABASE_NAME = "data.db";


private void copyDatabaseFromAssets() {
    try {

        byte[] buffer = new byte[1024];
        OutputStream myOutput;
        int length;
        InputStream myInput;

        String DB_PATH = this.getDatabasePath(AppSettings.DATABASE_NAME).getAbsolutePath();

        AssetManager assetManager = getAssets();
        myInput = assetManager.open("databases/" + AppSettings.DATABASE_NAME);

        myOutput = new FileOutputStream(DB_PATH);
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.close();
        myOutput.flush();
        myInput.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
live-love
  • 48,840
  • 22
  • 240
  • 204