0

可能重复:
如何从联系人的 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
4

2 回答 2

0

使用此代码从联系人中获取照片............

public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
// InputStream input = ContactsContract.Contacts.Photo
if (input == null) {
    return null;
}
return BitmapFactory.decodeStream(input);
于 2012-06-18T07:04:47.680 回答
0

在你想要的地方写这个片段

// set the profile picture
ImageView profile = (ImageView) findViewById(R.id.display_contact_image);
Bitmap bitmap = loadContactPhoto(getContentResolver(), _id);
if(bitmap == null) {
    //Set default contact image
    profile.setImageResource(R.drawable.default_contact_image);
} else {
    profile.setImageBitmap(bitmap);
}

方法是

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

并分享您尝试过的代码......(从上周开始:)

于 2012-06-18T07:36:41.330 回答