[英]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. 使用以下代码将资产文件夹中的sqlite文件复制到数据库文件夹。 i found this example here find CommonsWare's answer in this question
我发现这个例子在这个问题中找到了CommonsWare的答案
But im getting java.io.FileNotFoundException: /file:/android_asset/pg558.sqlite (No such file or directory) 但我得到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();
}
Please check you pg558.sqlite file, I've tried you code and it worked for my file. 请检查你pg558.sqlite文件,我已经尝试过你的代码,它适用于我的文件。 I copied a xml file from assets/ to /mnt/sdcard/.
我将一个xml文件从assets /复制到/ mnt / sdcard /。
To copy a file from assets folder to /databases folder: 要将文件从assets文件夹复制到/ 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.