Honeycomb 上的 db4o Db4oException:文件格式不兼容

发布于 2024-11-30 02:02:48 字数 1335 浏览 1 评论 0原文

我在一个小项目中使用 db4o,该项目在 Android 2.2、2.3 等上运行良好。但是,在 Honeycomb 上,数据库初始化会导致以下错误:

com.db4o.ext.Db4oException: File format incompatible: '/data/data/com.myapp/app_data/my_app.db4o'

强制关闭,并且该错误发生在运行 Honeycomb 的 Xoom 和 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 的权限?

I'm using db4o in a small project that works great on Android 2.2, 2.3, etc. On Honeycomb, however, database initialization results in the following error:

com.db4o.ext.Db4oException: File format incompatible: '/data/data/com.myapp/app_data/my_app.db4o'

This force close and the error occur on both a Xoom running Honeycomb and a Galaxy Tab running Honeycomb.

The relevant code is:

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;
}

Is there some difference in the way Honeycomb handles its external file system? Is there anything I can change in the db4oDBFullPath() method that would make the two compatible? I'm really at a loss as to what's happening that's different. Maybe there are some Honeycomb-specific permissions that I need to enable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁拥 2024-12-07 02:02:49

我在 Xoom 上看到了类似的东西。事实证明,文件创建出错是因为 Xoom 在严格模式下运行,而严格模式阻止了 DB4o 打电话回家。留下损坏的存根数据库。尝试在 AsyncTask 中执行初始 Db4oEmbedded.openFile 操作。

当然是删除损坏的文件后。

I saw something similar on a Xoom. Turns out file creation was erring out because the Xoom runs in strict mode and strict mode was preventing DB4o from phoning home. Leaving a corrupted stub database. Try doing your initial Db4oEmbedded.openFile inside an AsyncTask.

After deleting the corrupted file of course.

感悟人生的甜 2024-12-07 02:02:49
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
com.db4o.ext.Db4oException: File format incompatible

dissapears when I include in the AndroidManifest.xml the next line:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />;

it is important to mention that additionally I made the code for dealing with the db4o in separate thread...

so the code looks like:

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();
                }


            }
        });
    }
}

and the AndroidManifest.xml is:

<?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>

the debug output is:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文