Often we come across situation where we need to copy a database over an existing database(Usually Empty). Here is a sample code to copy our database from the Application’s assets folder over an empty DB for use

void copyDatabase() throws IOException{
InputStream myInput = myContext.getAssets().open(DBNAME);
OutputStream myOutput = new FileOutputStream(DBPATH+DBNAME);
byte[ ] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}

Incoming search terms:

  • android asset manager copy db form assets
  • copy database from asset to data/data/databases

Tagged with:

Filed under: Android

Like this post? Subscribe to my RSS feed and get loads more!