使用以下代码将sqlite文件从asset文件夹复制到数据库文件夹。我在这里找到了这个例子 find CommonsWare's answer in this question
但我得到java.io.FileNotFoundException:/ file:/android_asset/pg558.sqlite(没有这样的文件或目录)
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();
}
答案 0 :(得分:2)
请检查你的pg558.sqlite文件,我已经尝试过你的代码,它适用于我的文件。我将一个xml文件从assets /复制到/ mnt / sdcard /.
答案 1 :(得分:0)
要将文件从资产文件夹复制到/ databases文件夹,请执行以下操作:
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();
}
}