【发布时间】:2015-03-31 23:02:26
【问题描述】:
我有一个带有预加载数据库的应用程序。
我的临时复制功能是这样的
private void copyDatabase() {
AssetManager assetManager = context.getResources().getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(DATABASE_NAME);
out = new FileOutputStream(DATABASE_FILE);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) > 0){
out.write(buffer, 0, read);
}
} catch (IOException e) {
Log.e("error", e.getMessage());
} finally {
if(in != null) {
try {
in.close();
}catch (IOException e) {
Log.e("error", e.getMessage());
}
}
if(out != null) {
try {
out.close();
}catch (IOException e) {
Log.e("error", e.getMessage());
}
}
}
}
但在 Android 2.2 上,我不断收到错误消息“数据库磁盘映像格式错误”。所以我继续把它从设备上复制到我的电脑上。果然,它不会打开。我对这两个文件进行了十六进制比较,有 10 个实例中 1 个字节不同。 Hex 0D 已在畸形副本的随机位置添加了 10 次。
复制例程在 3.x+ 中运行良好。我也用相同的方法开发了其他应用程序,没有问题。
有什么想法吗?
【问题讨论】:
-
尝试使用 SQLiteAssetHelper github.com/jgilfelt/android-sqlite-asset-helper - 如果您按照说明对数据库进行 ZIP 或 GZIP 压缩,它将防止 Android 版本低于 API 10 的数据库损坏,并且它还可以完成所有“繁重的工作”关于数据库复制和升级。
-
这很奇怪。我使用完全相同的数据重新创建了我的预加载数据库,现在没有发生错误,并且复制良好。
-
这听起来好像文件是通过 FTP 以 ASCII(文本)模式复制的。
-
您的代码没问题,我无法相信您的问题。
Hex 0D has been added 10 times in random spots in the malformed copy。您没有在 0x0A 旁边找到它们吗? -
@greenapps 是的,他们在 0X0A 旁边,但我不知道为什么 android 应该这样做。
标签: android sqlite android-2.2-froyo