[英]Using ViewBinder, SimpleCursorAdapter, SQLite, to show pictures taken by a camera on ListView
[英]SQLite BLOB column to SimpleCursorAdapter with ViewBinder
我正在尝试显示当前存储在SQLiteDatabase
中的联系人列表。 以前,我已经检索了ContactsContract.Contacts.PHOTO_THUMBNAIL_URI
并将其以byte[]
的形式存储在数据库行的BLOB
列中。
现在,当我试图提取缩略图回来,由他们进行解码,以Bitmaps
在MyBinderView
类,图片不会出现,相反,我看空的空间(默认图像, 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;
}
}
任何帮助,将不胜感激。
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI
提供可以通过其检索的缩略图的路径。 因此,在了解到您可以通过调用parse
函数使用此路径构建URI
之后。
接下来,您将在此嵌入式类的帮助下查询新的uri-
private static class PhotoQuery {
public static final String[] PROJECTION = {
Photo.PHOTO
};
public static final int PHOTO = 0;
}
使用下面的代码,您将提取解决问题的所需byte []。
获得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;
}
希望它能帮助某人加油:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.