2011-04-12 67 views
1

我有一个应用程序需要获取给定联系人的照片。我有联系人的电话号码,那么有没有办法通过ContentResolver或任何其他形式通过电话号码检索联系人的照片?我一直在寻找,但没有找到答案。Android |通过电话号码检索联系人照片的最佳方式是什么?

我真的很想强调使用电话号码获取联系人照片(如果存在)的重要性。谢谢。

回答

8

好吧,我终于想出了如何添加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; 
} 
+2

欢呼起来ROFLwTIME,我尝试过很多联系,但couldnot发现任何相关信息,但您的帖子中奖 – 2012-04-11 05:53:54

+0

这就是我要找的。大奖!谢谢@ROFLwTIME – ajdeguzman 2014-03-25 02:13:31

0

我不认为有一个简单的方法来做到这一点。但我想你可以通过电话上的联系人,并检查每个数字是否是你想要匹配的数字。

由于无论如何数字是无序的,所以没有办法让它更有效率。在无序列表中搜索是通过遍历它们来执行的,因为我建议你应该处理这个问题。除非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(); 

这不是你的任务的完整解决方案,但通过修改上面的代码,我想你会得到通过。 :-)

相关问题