【问题标题】:How do I display a contact's photo from the contact's id?如何从联系人的 id 显示联系人的照片?
【发布时间】:2012-06-13 08:28:02
【问题描述】:

此代码(在我的 CustomAdapter 类中)仅显示基于谁向我发送短信并将其放入 ArrayList 的联系人 ID,然后显示列表。

每个联系人 ID 旁边都有一个名为 holder.photo 的 ImageView。 如何在 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);

目前,我的列表视图显示如下:

  • android_icon-----John Doe
  • android_icon-----简·史密斯
  • android_icon-----Foo Barr

【问题讨论】:

    标签: android imageview contacts photo


    【解决方案1】:

    试试这个..

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

    获取位图图像

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多