您好我正在构建一个使用12Mb数据库的小应用程序。 由于db大小我必须从资产文件夹中复制它,我使用字节数组将数据复制到空文件:
@Override
public void onCreate(SQLiteDatabase db){
try{
//Getting the asset db
InputStream myInput = this.context.getAssets().open("dicoMots.db");
//Creating the empty db to receive bytes
String outFileName = "/data/data/"+this.context.getApplicationContext().getPackageName()+"/databases/dicoMots.db";
OutputStream myOutput = new FileOutputStream(outFileName);
//Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[14336000];
int length;
while ((length = myInput.read(buffer)) > 0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
Log.d("DICO", "Database loaded");
}
catch(Exception e){
Log.d("DICO", "Database load fail");
}
}
已成功创建空文件,并且还会重新输入输入文件。 myInput.read返回的值也是正确的。 但输出文件仍为空,没有数据被复制。
答案 0 :(得分:0)
这种方法对我有用
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}