可能重复:
如何从联系人的 ID 显示联系人的照片?
一个多星期以来,我一直在尝试使用我设备中的联系人照片在我的 ListView 中填充我的 ImageView,但无济于事。
是否有针对API 级别 10执行此操作的完整解决方案?
我的 LogCat 代码:
** CustomAdapter 类:**
public class CustomAdapter extends ArrayAdapter<String> {
Cursor c;
String TAG = "CustomAdapter";
private Context context = null;
ArrayList<String> elements = null;
private ArrayList<String> data = null;
public static String contactName;
public static int count = 0;
private ArrayList<Boolean> itemChecked = null;
public static List<String> messages;
public static List<String> contactID;
String body;
String phoneNumber;
public CustomAdapter(Context context, int type, ArrayList<String> elements) {
super(context, type, elements);
data = elements;
this.elements = elements;
this.context = context;
}
// THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder {
public ImageView photo;
public TextView contact;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
final ViewHolder holder;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.contact_entry, null, false);
holder = new ViewHolder();
holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
holder.contact = (TextView) rowView
.findViewById(R.id.contactEntryText);
rowView.setTag(holder);
// RETRIEVE LATEST CONTACTS WHO SENT SMS (for visual)
contactID = new ArrayList<String>();
contactID = elements;
String folder = "content://sms/inbox/";
Uri mSmsQueryUri = Uri.parse(folder);
contactID = new ArrayList<String>();
try {
c = context.getContentResolver().query(
mSmsQueryUri,
new String[] { "_id", "thread_id", "address", "date",
"body" }, null, null, null);
if (c == null) {
Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
}
c.moveToFirst();
while (c.moveToNext()) {
phoneNumber = c.getString(0);
contactID.add(phoneNumber);
}
} catch (Exception e) {
// Log.e(TAG, e.getMessage());
} finally {
c.close();
}
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
// bind the data to the row views
holder.contact.setText(data.get(position));
holder.photo.setImageBitmap(getByteContactPhoto(contactID
.get(position)));
// SHOW CONTACT PHOTO IF IT EXISTS. IF NOT, DEFAULT (***NOT WORKING***)
Long l = Long.parseLong(contactID.get(position));
contactPhoto = loadContactPhoto(context.getContentResolver(), l);
if(contactPhoto == null){
holder.photo.setImageResource(R.drawable.ic_intel);
} else{
holder.photo.setImageBitmap(contactPhoto);
}
return rowView;
} // end if
// GET CONTACT PHOTO
private 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);
}
} // end class