使用带有BLOB数据的游标加载器

时间:2013-06-30 15:19:34

标签: java android android-sqlite

一个月前我问了一个类似的问题,但没有真正得到一个有帮助的答案,所以我想知道我是否可以更好地说出这个问题。

我正在使用一个将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

我知道这是因为我试图将其作为一个字符串读取,就像其他一切一样,但是我对使用这种方法正确抓取它感到茫然。我正在将数据加载到listactivity的行中。

任何人都可以帮我指出正确的方向吗?我在网上处理游标加载器和列表视图时没有找到很多。

由于

更新 - 我为简单游标适配器创建了一个新的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 个答案:

答案 0 :(得分:1)

SimpleCursorAdapter使用cursor.getString从数据库中检索值,如the source code中所示。对于图像,它假设它检索的字符串值是指向特定图像资源的整数(即,调用v.setImageResource(Integer.parseInt(value)))。因此,SimpleCursorAdapter无法理解如何处理blob数据,并且您需要使用自己的CursorAdapter类来执行从blob到Bitmap的正确转换以设置到您的{ {1}},可能使用ImageView中的以下代码:

bindView