如何显示联系人的照片从接触的id? [英] How do I display a contact's photo from the contact's id?

查看:169
本文介绍了如何显示联系人的照片从接触的id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code(在我的 CustomAdapter 班)只显示基础上,谁给我发短信,并将它们放入一个ArrayList的接触式ID,然后显示在列表中。

This code (within my CustomAdapter class) displays only the contact id based on the who sent me text messages and puts them into an ArrayList, then displays the list.

我有一个名为ImageView的 holder.photo 旁边的每个联系人的ID。 我怎么会去在ImageView的显示联系人的照片中的?

I have a ImageView called holder.photo next to each contact id. How would I go about displaying the contact's photo in the in the ImageView?

        String folder = "content://sms/inbox/";
        Uri mSmsQueryUri = Uri.parse(folder);
        messages = new ArrayList<String>();
        contactID = new ArrayList<String>();
        SMS = 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);
            }

        } catch (Exception e) {
            //Log.e(TAG, e.getMessage());
        } finally {
            c.close();
        }
            c.moveToFirst();
            while (c.moveToNext()) {

                phoneNumber = c.getString(0);
                contactID.add(phoneNumber);
            }
        holder.photo.?????
        //contact will cycle through all names and display each in a listview.
        holder.contact.setText(contactID.get(position);

目前,我的ListView显示这样的:

Currently, my listview displays this:

  • android_icon -----李四
  • android_icon -----珍妮·史密斯
  • android_icon -----富巴尔

推荐答案

试试这个..

public void getContacts(ContentResolver cr) {
    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String contactId = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

       Bitmap bitmap = loadContactPhoto(
                getContentResolver(), Long.valueOf(contactId))
       }
    phones.close();

得到位图图像

get Bitmap image

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

这篇关于如何显示联系人的照片从接触的id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆