如何把.db文件从资产数据/数据/包名/安卓 [英] How to put .db file from assets to data/data/packagename/ android
本文介绍了如何把.db文件从资产数据/数据/包名/安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application\">How使用现有的数据库,一个Android应用程序
如何把从资产数据/数据/包名.db文件/不使用的资产.db文件复制的内容。我不希望创建数据库,因为其用武之地把.db文件的资产。我探讨,但所有人都再次创造DATABSE但我只希望直接把那.db文件。
how to put .db file from assets to data/data/packagename/ without using copying content of .db file in assets. I don't want to create database because its useless to put the .db file in assets. I explore on it but all are again creating databse but i only want to put that .db file directly.
推荐答案
使用
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();
}
这篇关于如何把.db文件从资产数据/数据/包名/安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文