可能重复:
How to use an existing database with an Android application
如何将.db文件从资产放入data / data / packagename /,而不使用资产中的.db文件复制内容。我不想创建数据库,因为将.db文件放在资产中是没用的。我探索它,但所有人都在创建数据库,但我只想直接放入.db文件。
答案 0 :(得分:8)
使用此
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = "/data/data/"
+getApplicationContext().getPackageName()
+ "/databases/" + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}