我有一个列表视图,目前由联系人(目前为6人)填充,他们已在我设备的收件箱中发送短信。收集所有联系人并将其传递到ArrayList<String>
后,ArrayList将传递到我的 CustomAdapter 类的构造函数中。从那里开始,这是用我的getView()
方法中的收件箱中的联系人填充我的列表视图的代码:
holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
holder.contact = (TextView) rowView
.findViewById(R.id.contactEntryText);
String folder = "content://sms/inbox/";
Uri mSmsQueryUri = Uri.parse(folder);
contactID = new ArrayList<String>();
try {
c = context.getContentResolver().query(mSmsQueryUri,
new String[] { "_id", "address", "date", "body" },
null, null, null);
if (c == null) {
Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
}
c.moveToFirst();
while (c.moveToNext()) {
cid = c.getString(0);
contactID.add(cid); // stores contact IDs
}
} catch (Exception e) {
//Log.e(TAG, e.getMessage());
} finally {
c.close();
}
if(holder != null){
holder.contact.setText(data.get(position)); // displays contact by name
//Contact photo not showing
holder.photo.setImageBitmap(getByteContactPhoto(contactID.get(position));
}
从上面的代码中,holder.contact
显示6个联系人没有问题。但问题在于holder.photo
,它根本不显示任何内容。以下是获取照片的方法:
public Bitmap getByteContactPhoto(String contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, Long.parseLong(contactId));
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] {Contacts.Photo.DATA15}, null, null, null);
if (cursor == null) {
return null;
}
try {
cursor.moveToFirst();
if (cursor.moveToNext()) {
byte[] data = cursor.getBlob(0);
if (data != null) {
return BitmapFactory.decodeStream( new ByteArrayInputStream(data));
}
}
} finally {
cursor.close();
}
return null;
}
但在LogCat中,这是参考照片唯一显示的内容:
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
W/Resources(15031): Converting to string: TypedValue{t=0x12/d=0x0 a=3 r=0x7f080015}
有关修复此问题以便显示联系人照片的任何想法吗?