1. 程式人生 > >android 通過手機號查詢聯絡人的頭像

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;
}