本文介绍了Android的从通话记录中获取联系人图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是pretty轻松查询时得到联系人图片 People.CONTENT_URI ,用一个简单的

  People.loadContactPhoto(活动,ContentUris.withAppendedId(People.CONTENT_URI,使用ContactID)

因为我知道联系人ID。现在我需要做的accesing通话记录后,同样的事情。附:

 的String [] = strFields {
                android.provider.CallLog.Calls.CACHED_NAME,
                android.provider.CallLog.Calls.NUMBER,
                };        字符串strUriCalls =内容:// call_log /电话;            乌里UriCalls = Uri.parse(strUriCalls);            光标cursorLog = this.getContentResolver()查询(UriCalls,strFields,NULL,NULL,NULL);

我从通话记录列表中,但我不能找到加载照片所需要的接触ID联系起来的任何方式。该应用程序的工作原理,从API级别4 +。

任何帮助是AP preciated。谢谢你。

解决方案,由克里斯蒂安以下,这对我的作品有导游为:

 私人长期getContactIdFromNumber(串号){
        的String [] =投影新的String [] {} Contacts.Phones.PERSON_ID;
        乌里contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,Uri.en code(数字));
        光标C = getContentResolver()查询(contactUri,投影,NULL,NULL,NULL);        如果(c.moveToFirst()){
            长的ContactID = c.getLong(c.getColumnIndex(Contacts.Phones.PERSON_ID));
            返回的ContactID;
        }
        返回-1;
    }


解决方案

然后,你必须尝试通过查询通话记录字段,以获取联系人的ID。所以,你可以实现这样的:

 私人字符串getContactIdFromNumber(串号){
    的String [] =投影新的String [] {} Contacts.Phones._ID;
    乌里contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
        Uri.en code(数字));
    光标C = getContentResolver()查询(contactUri,投影,
        NULL,NULL,NULL);
    如果(c.moveToFirst()){
        字符串的ContactID = c.getString(c.getColumnIndex(Contacts.Phones._ID));
        返回的ContactID;
    }
    返回null;
}

然后,您可以使用接触式ID来获取照片。像这样的事情在你的情况:

  cursorLog.moveToFirst();
串号= cursorLog.getString(cursorLog.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
的ContactID = getContactIdFromNumber(数)
People.loadContactPhoto(活动,ContentUris.withAppendedId(People.CONTENT_URI,使用ContactID);
// 等等等等等等

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

People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId)

because I knew the contact Id. Now I need to do the same thing after accesing the Call log. With:

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

I get the list from call log, but I can't find any way of linking this with the contact id needed to load the photo. The app works from api level 4+.

Any help is appreciated. Thank you.

The solution, as guided by Cristian below, that works for me is:

 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;
    }
解决方案

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

这篇关于Android的从通话记录中获取联系人图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:49