SQLite BLOB列到SimpleCursorAdapter与ViewBinder

时间:2012-03-31 00:16:29

标签: android sqlite blob android-imageview android-viewbinder

我正在尝试显示当前存储在SQLiteDatabase中的联系人列表。 以前我检索过ContactsContract.Contacts.PHOTO_THUMBNAIL_URI并将其以byte[]的形式存储在我的数据库行的BLOB列中。

现在,当我尝试将缩略图提取回来时,通过将它们解码为Bitmaps类中的MyBinderView,图片不会显示,而是会看到空格(默认图片,{ {1}},显示正确)。我的ic_launcher行布局:

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
正确插入照片的

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

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

ContactsContract.Contacts.PHOTO_THUMBNAIL_URI

提供可以检索的缩略图的路径。 因此,在了解您通过调用URI函数来使用此路径构建parse之后。

接下来,您将在此嵌入式类的帮助下查询新的uri -

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

    public static final int PHOTO = 0;
}

使用下面的代码,您将提取解决问题的所需字节[]。

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

希望它会帮助某人 干杯:)