Android Get Contact Picture from Call Log

后端 未结 5 1242
南笙
南笙 2020-12-18 13:07

It was pretty easy to get the Contact picture when querying the People.CONTENT_URI, with a simple

People.loadContactPhoto(activity, ContentUr         


        
5条回答
  •  生来不讨喜
    2020-12-18 13:51

    Then, you must try to get the contact ID by using the queried call log fields. So, you can implement something like this:

    private String getContactIdFromNumber(String number) {
        String[] projection = new String[]{Contacts.Phones._ID};
        Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
            Uri.encode(number));
        Cursor c = getContentResolver().query(contactUri, projection,
            null, null, null);
        if (c.moveToFirst()) {
            String contactId=c.getString(c.getColumnIndex(Contacts.Phones._ID));
            return contactId;
        }
        return null;
    }
    

    Then, you can use that contact ID to get the photo. Something like this in your case:

    cursorLog.moveToFirst();
    String number = cursorLog.getString(cursorLog.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
    contactId = getContactIdFromNumber(number)
    People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId);
    // blah blah blah
    

提交回复
热议问题