Android从通话记录中获取联系人图片

Ali*_*lin 3 android calllog contacts android-contacts

People.CONTENT_URI通过简单的查询, 查询联系人图片非常容易

People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId)
Run Code Online (Sandbox Code Playgroud)

因为我知道联系人ID.现在我需要在访问呼叫日志后做同样的事情.附:

String[] strFields = {
                android.provider.CallLog.Calls.CACHED_NAME,
                android.provider.CallLog.Calls.NUMBER, 
                };

        String strUriCalls="content://call_log/calls"; 

            Uri UriCalls = Uri.parse(strUriCalls); 

            Cursor cursorLog = this.getContentResolver().query(UriCalls, strFields, null, null, null);
Run Code Online (Sandbox Code Playgroud)

我从通话记录列表中,但我不能找到加载照片所需要的接触ID连接这一点任何方式.该应用程序适用于api level 4+.

任何帮助表示赞赏.谢谢.

在下面的Cristian的指导下,对我有用的解决方案是:

 private long getContactIdFromNumber(String number) {
        String[] projection = new String[]{Contacts.Phones.PERSON_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()) {
            long contactId=c.getLong(c.getColumnIndex(Contacts.Phones.PERSON_ID));
            return contactId;
        }
        return -1;
    }
Run Code Online (Sandbox Code Playgroud)

Cri*_*ian 8

然后,您必须尝试使用​​查询的呼叫日志字段获取联系人ID.所以,你可以实现这样的事情:

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;
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用该联系人ID获取照片.在你的情况下这样的事情:

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
Run Code Online (Sandbox Code Playgroud)