是否有一个解决方案,用2.3的联系人照片填充列表视图?

时间:2012-06-18 07:01:24

标签: java android android-listview android-contacts android-photos

  

可能重复:
  How do I display a contact's photo from the contact's id?

我一直在尝试用我的设备中的联系人照片在我的ListView中填充我的ImageViews,但无济于事。

是否有 COMPLETE解决方案 API Level 10 执行此操作?

我的LogCat代码:

Why are my contact photos not displaying in listview?

** 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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);
}

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