2011-08-16 33 views
2

我使用了db4o在在Android 2.2,2.3,等的Honeycomb,但是,數據庫初始化會導致以下錯誤的偉大工程的一個小項目:的db4o在Honeycomb Db4oException:文件格式不兼容

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

是否有蜂窩處理其外部文件系統的方式有些不同?有什麼我可以在db4oDBFullPath()方法中改變,使兩者兼容?我真的不知道發生了什麼,這是不同的。也許有一些我需要啓用的特定於Honeycomb的權限?

+0

這是你在同一個系統中創建的文件,或一個來自其他地方(例如,也許不同的db4o版本)? –

+0

它由程序在飛行中創建,而不是從別處導入。 – Rockmaninoff

+0

只是爲了確保:這個目錄是否存在?它真的應該有兩次'數據'的名字? –

回答

1

我在Xoom上看到類似的東西。因爲Xoom以嚴格模式運行,並且嚴格模式阻止DB4o打電話回家,所以文件創建錯誤。留下損壞的存根數據庫。嘗試在AsyncTask中執行初始Db4oEmbedded.openFile。

當然刪除了損壞的文件。

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.xm l爲:

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