【发布时间】:2012-02-29 18:46:51
【问题描述】:
我正在使用以下代码来获取联系人图片:
Contact c = new Contact("", "", "", null);
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,pCur.getLong(pCur.getColumnIndex(Contacts._ID)));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input != null) {
Bitmap photo = BitmapFactory.decodeStream(input);
c.setContactImage(photo);
}
contacts.add(c);
我使用以下代码将联系信息添加到适配器类中的视图中:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder.img = (ImageView)convertView.findViewById(R.id.imgContact);
Contact entry = getItem(position);
if(entry.getContactImage()!=null){
holder.img.setImageBitmap(entry.getContactImage());
}
}
static class ViewHolder {
TextView name;
TextView number;
ImageView img;
}
清单有以下内容:
<uses-sdk android:minSdkVersion="9" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
这似乎在模拟器上完美运行,但是当我在我的 android 设备上测试它时它不起作用。我以同样的方式将图像添加到我的联系人中,模拟器和设备都是 2.3,设备是 HTC sense XL。
我从联系人列表中获得的其他信息在这两种情况下都非常适用。有人有解决这个问题的办法吗?
怎么做:
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
这是你调用函数的方式:
c.setContactImage(loadContactPhoto(cr, pCur.getLong(pCur.getColumnIndex(CommonDataKinds.Photo.CONTACT_ID))));
【问题讨论】: