首先设置需要获取的数据
private static final String[] PHONES_PROJECTION = new String[] {
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
其中上面的photo_id可以判断是否存在头像,当大于0是表示存在头像。
通过下面查询数据:
ContentResolver resolver = mContext.getContentResolver();
// 获取手机联系人
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
可以通过下面的代码获取photo_id:
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
然后通过photo_id进行判断,如果存在的话,就获取,不存在的话,就设置默认头像
if(photoid > 0 ) {
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
}
其中如果想将bitmap与string互转,可以查看这个网址:http://blog.csdn.net/h_025/article/details/71124493