SQLite的BLOB列与ViewBinder到SimpleCursorAdapter [英] SQLite BLOB column to SimpleCursorAdapter with ViewBinder

查看:371
本文介绍了SQLite的BLOB列与ViewBinder到SimpleCursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示当前存储在 SQLiteDatabase 联系人列表。
previously我取回 ContactsContract.Contacts.PHOTO_THUMBNAIL_URI 并将其存储在的一种形式的byte [] 中在 BLOB 列我的数据库行。

I'm trying to display a list of contacts that are currently stored in a 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.

现在,当我试图提取缩略图回来,在 MyBinderView 他们进行解码,以位图类,图片不出现,相反,我看空的空间(默认的图像, ic_launcher ,正确显示)。我的的ListView 行布局:

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). My ListView row layout:

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


}

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

ContactsContract.Contacts.PHOTO_THUMBNAIL_URI

提供到可以由被检索缩略图的路径。
因此,了解你建立后, 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.

下次你查询你的新的URI这个嵌入类的帮助 -

next you query your new uri with the help of this embedded class -

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

    public static final int PHOTO = 0;
}

使用code吼叫,你会提取解决问题所需的byte []。

using the code bellow you'll extract the needed byte[] that solved the issue.

字节获得[]是能够将其存储在您的数据库,并操纵它的以后需要的时候点。

the point of getting byte[] is to be able to store it in your DB and manipulate it later on when needed.

    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 :)

这篇关于SQLite的BLOB列与ViewBinder到SimpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆