简体   繁体   English

使用ViewBinder将SQLite BLOB列转换为SimpleCursorAdapter

[英]SQLite BLOB column to SimpleCursorAdapter with ViewBinder

I'm trying to display a list of contacts that are currently stored in a SQLiteDatabase . 我正在尝试显示当前存储在SQLiteDatabase中的联系人列表。 Previously I've retrieved ContactsContract.Contacts.PHOTO_THUMBNAIL_URI and stored it in a form of byte[] in the BLOB column of my database rows. 以前,我已经检索了ContactsContract.Contacts.PHOTO_THUMBNAIL_URI并将其以byte[]的形式存储在数据库行的BLOB列中。

Now when I'm trying to extract the thumbnails back, by decoding them to Bitmaps in MyBinderView class, the pictures don't appear, instead I see empty spaces(the default image, ic_launcher , is showed correctly). 现在,当我试图提取缩略图回来,由他们进行解码,以BitmapsMyBinderView类,图片不会出现,相反,我看空的空间(默认图像, ic_launcher ,正确显示)。 My ListView row layout: 我的ListView行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="1dp">

    <ImageView
        android:id="@+id/thumbnail"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="50dp"
        android:layout_marginRight="1dp"    
        android:src="@drawable/ic_launcher"/>
    <TextView
        android:id="@+id/email"
        android:layout_gravity="center_horizontal|center_vertical"
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"/>

</LinearLayout>

ListFragment class: ListFragment类:

//DataBaseHelper.PHOTO contains a BLOB fetched from sqlite database
//DataBaseHelper.NAME is a String (no problem here)
String[] from = { DataBaseHelper.PHOTO, DataBaseHelper.NAME };
int[] to = new int[] { R.id.thumbnail, R.id.email };

    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Give some text to display if there is no data. In a real
    // application this would come from a resource.
    setEmptyText("No E-mail buddies found");

    // We have a menu item to show in action bar.
    // setHasOptionsMenu(true);

    contacts = new DataBaseHelper(getActivity());
    contacts.open();

    // Create an empty adapter we will use to display the loaded data.
    mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.contacts,
            null, from, to);

    mAdapter.setViewBinder(new MyViewBinder());
    setListAdapter(mAdapter);

    // Start out with a progress indicator.
    setListShown(false);

    // Prepare the loader. Either re-connect with an existing one,
    // or start a new one.
    getLoaderManager().initLoader(0, null, this);
}

ViewBinder class for the photo to be inserted correctly: 要正确插入照片的ViewBinder类:

public class MyViewBinder implements ViewBinder{

    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.thumbnail:
            ImageView picture = (ImageView) view;

                byte[] blob = cursor.getBlob(columnIndex);
                if(blob!=null){
                picture.setImageBitmap(
                        BitmapFactory.decodeByteArray(blob, 0, blob.length)
                        );
                }
                else 
                    picture.setImageResource(R.drawable.ic_launcher);

            return true;
        }
        return false;
    }


}

Any help would be appreciated. 任何帮助,将不胜感激。

ContactsContract.Contacts.PHOTO_THUMBNAIL_URI

Provides a path to the thumbnail that can be retrieved by. 提供可以通过其检索的缩略图的路径。 So after understanding that you build a URI using this path by calling parse function. 因此,在了解到您可以通过调用parse函数使用此路径构建URI之后。

next you query your new uri with the help of this embedded class - 接下来,您将在此嵌入式类的帮助下查询新的uri-

    private static class PhotoQuery {
    public static final String[] PROJECTION = {
        Photo.PHOTO
    };

    public static final int PHOTO = 0;
}

using the code bellow you'll extract the needed byte[] that solved the issue. 使用下面的代码,您将提取解决问题的所需byte []。

the point of getting byte[] is to be able to store it in your DB and manipulate it later on when needed. 获得byte []的目的是能够将其存储在您的数据库中,并在以后需要时对其进行操作。

    private byte[] getImage(String uriString){

    if(uriString==null)
        return null;
    Uri myuri = Uri.parse(uriString);

    Cursor photoCursor = getContentResolver().query(myuri, PhotoQuery.PROJECTION, null, null, null);

    if (photoCursor != null) {
        try {
            if (photoCursor.moveToFirst()) {
                final byte[] photoBytes = photoCursor.getBlob(PhotoQuery.PHOTO);
                if (photoBytes != null) {
                 return photoBytes;
             }
            }
        } finally {
            photoCursor.close();
        }
    }

    return null;
}

Hope it'll help someone cheers :) 希望它能帮助某人加油:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM