Android |通过电话号码检索联系人照片的最佳方法是什么?

| 我有一个需要获取给定联系人照片的应用程序。我有该联系人的电话号码,如果通过ContentResolver或其他任何形式的电话号码存在该联系人的照片,是否可以检索该照片?我一直在搜索,但没有找到答案。 我真的很想强调使用电话号码获取联系人照片(如果有)的重要性。谢谢。     
已邀请:
好的,我终于弄清楚了如何添加Facebook照片。此方法将通过电话号码添加您需要的Facebook照片:
    public Bitmap getFacebookPhoto(String phoneNumber) {
    Uri phoneUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Uri photoUri = null;
    ContentResolver cr = this.getContentResolver();
    Cursor contact = cr.query(phoneUri,
            new String[] { ContactsContract.Contacts._ID }, null, null, null);

    if (contact.moveToFirst()) {
        long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID));
        photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);

    }
    else {
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image);
        return defaultPhoto;
    }
    if (photoUri != null) {
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                cr, photoUri);
        if (input != null) {
            return BitmapFactory.decodeStream(input);
        }
    } else {
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image);
        return defaultPhoto;
    }
    Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_report_image);
    return defaultPhoto;
}
    
我认为没有简单的方法可以做到这一点。但是我想您可以通过电话上的“通讯录”检查每个号码是否与您尝试匹配的号码相同。 由于数字是无序的,因此无法提高效率。通过遍历它们来执行无序列表中的搜索,因为我建议您应该解决此问题。除非Android将所有数字保存在我不知道的有序列表中。 您可以通过以下方式遍历“联系人”:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
while (cursor.moveToNext()) { 
   String contactId = cursor.getString(cursor.getColumnIndex( 
   ContactsContract.Contacts._ID)); 
   String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
   if (Boolean.parseBoolean(hasPhone)) { 
      // You know it has a number so now query it like this
      Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +\" = \"+ contactId, null, null); 
      while (phones.moveToNext()) { 
         String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
      } 
   phones.close(); 
}
cursor.close();
这不是您任务的完整解决方案,但是通过修改上面的代码,我想您会成功的。 :-)     

要回复问题请先登录注册