我在一个在Android 2.2,2.3等上运行良好的小项目中使用db4o。但是,在Honeycomb上,数据库初始化会导致以下错误:
com.db4o.ext.Db4oException: File format incompatible: '/data/data/com.myapp/app_data/my_app.db4o'
此力关闭并且在运行Honeycomb的Xoom和运行Honeycomb的Galaxy Tab上都会出现错误。
相关代码是:
public ObjectContainer db() {
// Create, open, and close the database
try {
if (oc == null || oc.ext().isClosed()) {
oc = Db4oEmbedded
.openFile(dbConfig(), db4oDBFullPath(mContext));
}
return oc;
} catch (Exception e) {
Log.e(CFAApplication.TAG, e.toString());
return null;
}
}
private String db4oDBFullPath(Context ctx) {
// Returns the path for the database location
return ctx.getDir("data", 2) + "/" + "myapp.db4o";
}
public List<MyItem> getListItem(final String passedItemTitle) {
List<MyItem> result = db().query(new Predicate<MyItem>() { // Error occurs here
public boolean match(MyItem listItem) {
if (passedItemTitle.equals(listItem.getTitle())) {
return true;
}
return false;
}
});
return result;
}
Honeycomb处理外部文件系统的方式有什么不同吗?我可以在db4oDBFullPath()方法中更改哪些内容使两者兼容吗?我真的不知道发生了什么不同。也许我需要启用一些特定于Honeycomb的权限?
答案 0 :(得分:4)
答案 1 :(得分:1)
我在Xoom上看到类似的东西。事实证明文件创建错误,因为Xoom以严格模式运行,严格模式阻止DB4o打电话回家。留下损坏的存根数据库。尝试在AsyncTask中执行初始的Db4oEmbedded.openFile。
当然删除损坏的文件后。
答案 2 :(得分:1)
com.db4o.ext.Db4oException: File format incompatible
当我在AndroidManifest.xml中包含下一行时,消失了:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />;
值得一提的是,另外我在单独的线程中编写了用于处理db4o的代码...
所以代码如下:
package com.inbytebg;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.inbytebg.entities.Customer;
public class MainActivity8 extends Activity {
Button savetodb4o;
Button readfromdb4o;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
savetodb4o = (Button) findViewById(R.id.save_to_db4o);
savetodb4o.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new Thread() {
@Override
public void run() {
String dbPath = getApplicationContext().getDir("data", 0) + "/" + "customer.db4o";
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
Customer cust = new Customer();
cust.setId(1);
cust.setName("stancho stanchev");
cust.setAddress("razgrad");
try {
db.store(cust);
Log.i("db4o...", "customer stored...");
} finally {
db.close();
}
}
}.start();
}
});
readfromdb4o = (Button) findViewById(R.id.read_from_db4o);
readfromdb4o.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String dbPath = getApplicationContext().getDir("data", 0) + "/" + "customer.db4o";
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded.newConfiguration(), dbPath);
ObjectSet result;
try {
result = db.queryByExample(Customer.class);
while (result.hasNext()) {
Customer customer = (Customer) result.next();
Log.i("db4o retrieve:...",
"Name: " + customer.getName()
+ " id: " + customer.getId()
+ " address: " + customer.getAddress());
}
} finally {
db.close();
}
}
});
}
}
和AndroidManifest.xml是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.inbytebg"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name="MainActivity8"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
调试输出是:
D/dalvikvm( 2231): GC_CONCURRENT freed 538K, 9% free 6705K/7367K, paused 7ms+14ms
I/db4o... ( 2231): customer stored...
I/db4o retrieve:...( 2231): Name: stancho stanchev id: 1 address: razgrad
I/db4o retrieve:...( 2231): Name: stancho stanchev id: 1 address: razgrad
I/db4o retrieve:...( 2231): Name: stancho stanchev id: 1 address: razgrad
I/db4o retrieve:...( 2231): Name: stancho stanchev id: 1 address: razgrad
I/db4o retrieve:...( 2231): Name: stancho stanchev id: 1 address: razgrad