2

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.

Community
  • 1
  • 1
User42590
  • 2,473
  • 12
  • 44
  • 85

1 Answers1

8

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();
}
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • 3
    i don't want to use that. i simply want to copy .db file in data/data/packagename – User42590 Jan 17 '13 at 13:07
  • 1
    That's what this is doing. There's no "copy" function available to invoke, so the program must actually write out a new file itself. The original is read-only so can't be "moved". And there is no way to package a writable copy in the .apk. – Chris Stratton Jan 17 '13 at 13:10
  • that is the only way to put db from asset... otherwise you have to make a database programetically.. – Sanket Kachhela Jan 17 '13 at 13:18
  • this gives me filenotfound exception in that directory. there is no folder named databases, – M D P Dec 12 '15 at 19:30