Database 将资产中的数据库复制到emulator中的数据/数据库工作,但不在设备中工作
我有这样的代码:在模拟器中工作,但在设备中失败。 我想将存储在资产中的数据库复制到data/data/。。。。加载应用程序时显示“错误文件”(FileNotFoundException e)->错误 我在emulator中运行并且工作得很好,但是在我的设备中不工作,emulator的操作系统是4.3,我的设备的操作系统是4.0.4(根目录)、4.0.4和4.3 DB的一行是: 14 11107 Ovalo con mancuernas 11 Hombros 3 12 10无干草datos Utiliza 曼库尔纳斯国际酒店 这是一个以比索为中心的运动。 特拉巴贾拉斯前区酒店 霍姆布罗。img11107_1.png img11107_2.png img11107_3.png 更新:Database 将资产中的数据库复制到emulator中的数据/数据库工作,但不在设备中工作,database,copy,assets,Database,Copy,Assets,我有这样的代码:在模拟器中工作,但在设备中失败。 我想将存储在资产中的数据库复制到data/data/。。。。加载应用程序时显示“错误文件”(FileNotFoundException e)->错误 我在emulator中运行并且工作得很好,但是在我的设备中不工作,emulator的操作系统是4.3,我的设备的操作系统是4.0.4(根目录)、4.0.4和4.3 DB的一行是: 14 11107 Ovalo con mancuernas 11 Hombros 3 12 10无干草datos Uti
我在下一条评论中提出了解决方案。最后我找到了de solution:
public void copyDB() {
String destPath = "/data/data/" + getPackageName() + "/databases/MiBD";
File f2 = new File(destPath);
try {
if (!f2.exists()) {
//create a empty file
InputStream iStream = getBaseContext().getAssets().open(
"datosejer");
AdaptadorBD db = new AdaptadorBD(getApplicationContext());
db.open();
db.close();
OutputStream oStream = new FileOutputStream(destPath);
//open a file that was created and write data.
db.open();
byte[] buffer = new byte[1024];
int length;
while ((length = iStream.read(buffer)) > 0) {
oStream.write(buffer, 0, length);
}
iStream.close();
oStream.close();
db.close();
}
} catch (IOException e1) {
e1.printStackTrace();
Toast.makeText(MainActivity.this, "Error.",
Toast.LENGTH_SHORT).show();
}
}
现在,在设备中工作您的解决方案是什么?我在emulatorI上遇到了相同的问题,但在emulatorI上,我更改了此copyDB的旧copyDB(),可能解决方案是:OutputStream oStream=new FileOutputStream(destPath)//打开已创建的文件并写入数据。db.open();字节[]缓冲区=新字节[1024];整数长度;while((length=iStream.read(buffer))>0){oStream.write(buffer,0,length);}iStream.close();oStream.close();db.close();嘿,谢谢,是的,我刚才听到了,我只是在复制之前没有打开数据库。
public void copyDB() {
String destPath = "/data/data/" + getPackageName() + "/databases/MiBD";
File f2 = new File(destPath);
try {
if (!f2.exists()) {
//create a empty file
InputStream iStream = getBaseContext().getAssets().open(
"datosejer");
AdaptadorBD db = new AdaptadorBD(getApplicationContext());
db.open();
db.close();
OutputStream oStream = new FileOutputStream(destPath);
//open a file that was created and write data.
db.open();
byte[] buffer = new byte[1024];
int length;
while ((length = iStream.read(buffer)) > 0) {
oStream.write(buffer, 0, length);
}
iStream.close();
oStream.close();
db.close();
}
} catch (IOException e1) {
e1.printStackTrace();
Toast.makeText(MainActivity.this, "Error.",
Toast.LENGTH_SHORT).show();
}
}