How to copy file from asset folder to .../database folder in android
'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 (开发者_JS百科(len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
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/.
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();
}
}
精彩评论