Android 通过手机号查找联系人的头像

/***
*
* @author lpYuqun
* @param context
* @param phoneNumber_MOBILE
* @return Bitmap
*
*/
public static Bitmap getAvatarByPhoneNumber(Context context,
    String phoneNumber_MOBILE) {

Bitmap bitmap = null;

ContentResolver contentResolver = context.getContentResolver();

String projection[] = new String[] { Phone.CONTACT_ID, Phone.NUMBER };

phoneNumber_MOBILE = PhoneNumberUtils.formatNumber(phoneNumber_MOBILE);

Cursor cursor = contentResolver.query(Phone.CONTENT_URI, projection, // select
     ContactsContract.CommonDataKinds.Phone.TYPE + "="
       + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE
       + " and " + Phone.NUMBER + " = ?", // where sentence
     new String[] { phoneNumber_MOBILE }, // where values
     null); // order by

if (cursor.moveToFirst()) {

String _contact_ID;
    int _contact_ID_Column = cursor.getColumnIndex(Phone.CONTACT_ID);

_contact_ID = cursor.getString(_contact_ID_Column);

Uri contactPhotoUri = ContentUris.withAppendedId(
      People.CONTENT_URI, Long.parseLong(_contact_ID));

InputStream is = People.openContactPhotoInputStream(
      contentResolver, contactPhotoUri);
    if (is != null) {
     try {
      bitmap = BitmapFactory.decodeStream(is);
      is.close();
     } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
  
   }
   return bitmap;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsxdf.html