今天偶尔遇到要获取联系人头像的问题,却发现简单的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) {
//....
}
* 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);
}
* 获取联系人高清头像
* @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);
}
非常感谢了,好用
话说为什么要联系人头像。
看着不腻吗。
崇拜安卓开发牛人。
哇!博主還會Android編程!厲害!!
这年头不什么都回点,怎么混啊
这个比较实用
但实际应用的时候不多吧
不错不错可以取图了
第一次来这,留个脚印
欢迎