0

我正在尝试显示当前存储在SQLiteDatabase. 以前我已经以数据库行的列ContactsContract.Contacts.PHOTO_THUMBNAIL_URI中的形式检索并存储它。byte[]BLOB

现在,当我尝试将缩略图提取回来时,通过BitmapsMyBinderView课堂上将它们解码,图片不会出现,而是看到空白(默认图像 ,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正确插入照片的类:

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


}

任何帮助,将不胜感激。

4

1 回答 1

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

希望它会帮助有人欢呼:)

于 2012-04-04T01:08:54.373 回答