Android 获取高清联系人头像

  今天偶尔遇到要获取联系人头像的问题,却发现简单的ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);不能满足要求了,取出来的头像居然是96*96的,显示全是马赛克。所以网上找了一通,没发现。

  最后Ctrl+B跳转到源代码去查看openContactPhotoInputStream的接口,发现了如下内容。


/**
 * Opens an InputStream for the contacts's thumbnail photo and returns the
 * photo as a byte stream.
 * @param cr The content resolver to use for querying
 * @param contactUri the contact whose photo should be used. This can be used with
 * either a {@link #CONTENT_URI} or a {@link #CONTENT_LOOKUP_URI} URI.
 * @return an InputStream of the photo, or null if no photo is present
 * @see #openContactPhotoInputStream(ContentResolver, Uri, boolean), if instead
 * of the thumbnail the high-res picture is preferred
 */

public static InputStream openContactPhotoInputStream(ContentResolver cr, Uri contactUri) {
    return openContactPhotoInputStream(cr, contactUri, false);
}

/**
 * Opens an InputStream for the contacts's photo and returns the
 * photo as a byte stream.
 * @param cr The content resolver to use for querying
 * @param contactUri the contact whose photo should be used. This can be used with
 * either a {@link #CONTENT_URI} or a {@link #CONTENT_LOOKUP_URI} URI.
 * @param preferHighres If this is true and the contact has a higher resolution photo
 * available, it is returned. If false, this function always tries to get the thumbnail
 * @return an InputStream of the photo, or null if no photo is present
 */

public static InputStream openContactPhotoInputStream(ContentResolver cr, Uri contactUri,
boolean preferHighres) {
        //....
}

  原始的参数竟然还有一个preferHighres,你说这让我情何以堪,问题是解决了,可犯了个不大不小的毛病,原本直接查文档可以解决的事情,非得百度谷歌一通,下次注意吧。所以现在获取高清头像的接口是这样的。


/**
 * 获取联系人高清头像
 * @param people_id 联系人ID
 * @param cr 调用容器
 * @return 联系人的高清头像
 */

public static Bitmap getHighPhoto(String people_id, ContentResolver cr) {
    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(people_id));
    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true);
    if (input == null) {
        return null;
    }
    return BitmapFactory.decodeStream(input);
}

获取安卓高清头像

10条评论在“Android 获取高清联系人头像”

写下你最简单的想法