|
这篇是补充我前面的一篇:获取联系人信息。那篇没有获取头像的代码,在这里补充。
/**
* 获取联系人头像
*
* @param people_id
* @return
*/
public byte[] getPhoto(String people_id) {
String photo_id = null;
String selection1 = ContactsContract.Contacts._ID + " = " + people_id;
Cursor cur1 = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, selection1, null,
null);
if (cur1.getCount() > 0) {
cur1.moveToFirst();
photo_id = cur1.getString(cur1
.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
Log.i(TAG, "photo_id:" + photo_id); // 如果没有头像,这里为空值
}
String selection = null;
if(photo_id == null){
return null;
}else{
selection = ContactsContract.Data._ID + " = " + photo_id;
}
String[] projection = new String[] { ContactsContract.Data.DATA15 };
Cursor cur = getContentResolver().query(
ContactsContract.Data.CONTENT_URI, projection, selection, null, null);
cur.moveToFirst();
byte[] contactIcon = cur.getBlob(0);
Log.i(TAG, "conTactIcon:" + contactIcon);
if (contactIcon == null) {
return null;
} else {
return contactIcon;
}
}
/**
* 设置联系人头像
* 如果有头像,就不会出错,如果没有头像,就会出错。1:正确 2:错误
*/
public void setPhoto() {
// 以下代码将字节数组转化成Bitmap对象,然后再ImageView中显示出来
ImageView image = null;
image = (ImageView) this.findViewById(R.id.imageview1);
String contactId = "1"; // 2
byte[] photo = getPhoto(contactId);
Bitmap map = BitmapFactory.decodeByteArray(photo, 0, photo.length);
image.setImageBitmap(map);
} |
|