How to get Android Contact thumbnail

别来无恙 提交于 2020-01-29 06:38:04

问题


I have a listview adapter and I'm trying the following in the newView method:

@Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        long contactId = Long.valueOf(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))); 
        String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        boolean hasPhone = Boolean.parseBoolean(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
        String thumbnailUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)); 

        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(contactName);
        }

        name_text.setTag(new Contact(contactId, hasPhone));

        ImageView thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
        if (thumbnailUri != null) {
            thumbnail.setImageURI(Uri.parse(thumbnailUri));
        } else {
            thumbnail.setImageResource(R.drawable.ic_launcher);
        }

        return v;
    }

But when I try to parse the Uri that is stored in thumbnailUri, I receive the following error:

   08-09 01:58:38.619: I/System.out(1471): resolveUri failed on bad bitmap uri: content://com.android.contacts/contacts/1/photo

Am I going about this the wrong way? Any help would be greatly appreciated!


回答1:


private Uri getPhotoUriFromID(String id) {
    try {
        Cursor cur = getContentResolver()
                .query(ContactsContract.Data.CONTENT_URI,
                        null,
                        ContactsContract.Data.CONTACT_ID
                                + "="
                                + id
                                + " AND "
                                + ContactsContract.Data.MIMETYPE
                                + "='"
                                + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                                + "'", null, null);
        if (cur != null) {
            if (!cur.moveToFirst()) {
                return null; // no photo
            }
        } else {
            return null; // error in cursor process
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    Uri person = ContentUris.withAppendedId(
            ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
    return Uri.withAppendedPath(person,
            ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}

This is the function where you need to pass contact id and you will get the URI of the image which you can set easily in the imageview.

use the response uri of this function like imageView.setImageURI(uri)

Hope it will work for your code.



来源:https://stackoverflow.com/questions/11866165/how-to-get-android-contact-thumbnail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!