开发者

Android Get Contact Picture from Call Log

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


This one works fine for me..

private void contactPickedFromLog(Intent data) {
    // TODO Auto-generated method stub
    String contactNumber = data.getStringExtra(CONTACT_NUMBER);
    Cursor cursor = getContentResolver().query(
            Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                    Uri.decode(contactNumber)),
            new String[] { PhoneLookup._ID }, null, null, null);
    if(cursor.moveToFirst()){
    long contactId = cursor.getLong(0);
    InputStream inputStream = Contacts.openContactPhotoInputStream(
            getContentResolver(),
            ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId));
    if(inputStream!=null)
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    }
}  


I'm doing it in this way:

ContentResolver cr=this.getContentResolver();
Cursor cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cc.moveToNext())
{
    contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID));
    Uri contactPhotoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
    InputStream is=ContactsContract.Contacts.openContactPhotoInputStream(cr, contactPhotoUri);
    //blah-blah
}


Try this ...

public Bitmap getPhoto(String phoneNumber) {
    Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Uri photoUri = null;
    ContentResolver cr = 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(), R.drawable.ic_contact_picture);
        return getCircleBitmap(defaultPhoto);
    }
    if (photoUri != null) {
        InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
                cr, photoUri);
        if (input != null) {
            return getCircleBitmap(BitmapFactory.decodeStream(input));
        }
    } else {
        Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
        return getCircleBitmap(defaultPhoto);
    }
    Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
    contact.close();
    return defaultPhoto;
}


All above answer is correct.You can also get photo by this...

c.getString(c.getColumnIndex(CallLog.Calls.CACHED_PHOTO_URI));

work in SDK >=23

if you are working with minimum SDK...

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(num));
        uri = ((phone_uri != null) ? Uri.parse(phone_uri) : uri);
        Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);

        if (cursor != null) {
            if (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
                String name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
                image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
                Log.d(TAG, "name " + name + " id "+id+" image_uri "+ image_uri);
            }
            cursor.close();
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜