2013-06-30 32 views
0

一個月前我問了一個類似的問題,但沒有真正得到有幫助的答案,所以我想看看我能否更好地說出問題。使用帶有BLOB數據的光標加載器

我正在使用將BLOB數據存儲在數據庫中的應用程序。數據是一張圖片,非常小(小於2KB)。我決定將它們直接戴在數據庫中,因爲它的大小。

我還在學習編程,我敢肯定我錯過了一些簡單的東西,但這是我的問題。我有很多文本字段的在我的數據庫,我用這個方法拉離它們的信息:

private void fillData() { 

    // Pulls the fields from the database and assigns them to the columns 

    String[] from = new String[] { BorrowMeTable.COLUMN_ITEM, 
      BorrowMeTable.COLUMN_NAME, BorrowMeTable.COLUMN_DATE }; 

    // The label for each textview 
    int[] to = new int[] { R.id.label, R.id.pName, R.id.pDate }; 

    getLoaderManager().initLoader(0, null, this); 
    adapter = new SimpleCursorAdapter(this, R.layout.bm_row, null, from, 
      to, 0); 

    setListAdapter(adapter); 
} 

我的類實現LoaderManager.LoaderCallbacks及所附方法如下:

public Loader<Cursor> onCreateLoader(int id, Bundle args) { 

    // Remove to show all records - This sets up the list to filter based on 
    // items returned 
    String mSelection = BorrowMeTable.COLUMN_BORROW_FLAG + "=?"; 

    // SQL request for only returned items to be shown 
    String[] selectionArgs = { "Borrowed" }; // The actual argument 

    String[] projection = { BorrowMeTable.COLUMN_ID, 
      BorrowMeTable.COLUMN_ITEM, BorrowMeTable.COLUMN_NAME, 
      BorrowMeTable.COLUMN_DATE }; 
    // mSelection & selectionArgs can be removed and set to null to display 
    // all records 
    CursorLoader cursorLoader = new CursorLoader(this, 
      BorrowMeContentProvider.CONTENT_URI, projection, mSelection, 
      selectionArgs, null); 
    return cursorLoader; 
} 

當我嘗試添加它具有BLOB數據列 - 我得到一個錯誤,說我不能一個blob轉換爲字符串:

06-30 15:13:36.152: E/AndroidRuntime(842): android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string 

我知道這是因爲我試圖像其他所有東西一樣,將它看作一個字符串,但是我不知道如何使用這種方法正確地抓住它。我將數據加載到列表活動的行中。

任何人都可以幫助指向正確的方向嗎?我還沒有發現很多在線處理光標加載器和列表視圖。

感謝

更新 - 我創建了一個新的bindview很簡單光標適配器,但我仍然得到以下錯誤消息:

07-02 20:42:53.350: E/AndroidRuntime(798): FATAL EXCEPTION: main 
07-02 20:42:53.350: E/AndroidRuntime(798): android.database.sqlite.SQLiteException:   unknown error (code 0): Unable to convert BLOB to string 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.CursorWindow.nativeGetString(Native Method) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.CursorWindow.getString(CursorWindow.java:434) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.database.CursorWrapper.getString(CursorWrapper.java:114) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.CursorAdapter.getView(CursorAdapter.java:250) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.AbsListView.obtainView(AbsListView.java:2159) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.ListView.onMeasure(ListView.java:1158) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
07-02 20:42:53.350: E/AndroidRuntime(798): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.View.measure(View.java:15518) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer.doFrame(Choreographer.java:532) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.os.Handler.handleCallback(Handler.java:725) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.os.Looper.loop(Looper.java:137) 
07-02 20:42:53.350: E/AndroidRuntime(798): at android.app.ActivityThread.main(ActivityThread.java:5041) 
07-02 20:42:53.350: E/AndroidRuntime(798): at java.lang.reflect.Method.invokeNative(Native Method) 
07-02 20:42:53.350: E/AndroidRuntime(798): at java.lang.reflect.Method.invoke(Method.java:511) 
07-02 20:42:53.350: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
07-02 20:42:53.350: E/AndroidRuntime(798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
07-02 20:42:53.350: E/AndroidRuntime(798): at dalvik.system.NativeStart.main(Native Method) 

這是我用我的看法粘結劑代碼:

public class MyViewBinder implements ViewBinder { 

@Override 
public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
    // TODO Auto-generated method stub 

    int viewId = view.getId(); 

    Log.i("ViewBinder: view", Integer.toString(viewId)); 
    Log.i("ViewBinder: name",cursor.getString(2)); 
    Log.i("ViewBinder: email",cursor.getString(3)); 
    Log.i("ViewBinder: photo",cursor.getBlob(4)==null?"NO Photo":"Has photo"); 

    switch(viewId){ 

    case R.id.pic_of_item: 

     ImageView storedItem = (ImageView) view; 

     byte[] blob = cursor.getBlob(columnIndex); 

     // If the blob is null then pull it from database 
     if (blob != null){ 

      // Decode the bitmap from the blob 
      storedItem.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length)); 

      Log.i("RETURN TRUE BLOB",cursor.getString(3)); 

     } else { 

      // If nothing is found use the camera icon from drawable 
      storedItem.setImageResource(R.drawable.take_camera_small); 

      Log.i("RETURN TRUE NO BLOB",cursor.getString(3)); 

      return true; 
     } 

    } 

    Log.i("RETURN FALSE",cursor.getString(3)); 

    return false; 
} 

}

回答

1

SimpleCursorAdapter使用cursor.getString從數據庫中檢索值,如the source code所示。對於圖像,它假定它檢索的字符串值是指向特定圖像資源的整數(即,調用v.setImageResource(Integer.parseInt(value)))。因此,SimpleCursorAdapter並不瞭解如何處理blob數據,您需要使用自己的CursorAdapter類,它可以從blob到Bitmap進行適當的轉換,以設置到您的ImageView,可能使用類似於bindView中的以下代碼的東西:

ImageView imageView = // get your ImageView 
byte[] blob = cursor.getBlob(cursor.getColumnIndex(BorrowMeTable.COLUMN_ITEM)); 
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length); 
imageView.setImageBitmap(bitmap); 
+0

我能夠使用的步驟從文章中創建自己的viewbinder [這裏](http://stackoverflow.com/questions/9951871/sqlite-blob-column-to-simplecursoradapter-with-viewbinder)但我仍然收到關於字符串到Blob的錯誤消息 – Vince

+0

我添加了上面的錯誤代碼。 – Vince

+0

@Vince - 所以你建立了一個'ViewBinder',它有一個開關尋找'BorrowMeTable.COLUMN_ITEM'?如果你的'ViewBinder'返回'false','getString'只能在'bindView'中調用。你確認(通過'Log'語句或調試)你的'ViewBinder'處理圖像並返回'true'? – ianhanniballake