4

i want to get a contact photo (if he has one) from the callLog.

now i know i can get the number and then query the contacts provider for a contact id.

however i want to know if there is a better way one that directly get the photo uri from the callLog.calls table. what makes me believe it might be possible is the fact that inside the documentation i ran across 2 interesting fields:

1)CACHED_LOOKUP_URI -The cached URI to look up the contact associated with the phone number, if it exists.

2)CACHED_PHOTO_ID - The cached photo id of the picture associated with the phone number, if it exists.

now if it can be done how, and if it cant be done than i would like to know what those fields are used for, thx

Gabriel H
  • 1,558
  • 2
  • 14
  • 35
  • @ Gabriel H have you find solution for this,because I am having same problem while loading image ,but loading photo from other cursor take time for multiple number – Sagar Sep 18 '17 at 10:49
  • @Sagar2869767 nope i left that specific project a while ago and unfortunatlly haven't really figured out a better way than getting the contact number and then the photo, i can only suggest to do this in an asynchronous way so to not jam the app with loading time (put a default pic and swap it) – Gabriel H Oct 09 '17 at 07:29
  • thanks for suggestion,for the while I am loading in asynchronous way .Still looking for better solution – Sagar Oct 09 '17 at 07:53

2 Answers2

1

You can get its ID. Then you have to retrieve the actual image from ContactsContract.

imageDataRow = c.getInt(c.getColumnIndex(CallLog.Calls.CACHED_PHOTO_ID));
Cursor c = mContext.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[]{
            ContactsContract.CommonDataKinds.Photo.PHOTO
    }, ContactsContract.Data._ID + "=?", new String[]{
            Integer.toString(imageDataRow)
    }, null);
    byte[] imageBytes = null;
    if (c != null) {
        if (c.moveToFirst()) {
            imageBytes = c.getBlob(0);
        }
        c.close();
    }
Bitmap photo = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
lotdrops
  • 300
  • 5
  • 16
0

Mostly, I have used Glide to set an image on view. SDK>=23

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

SDK<23

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

        if (cursor != null && cursor.moveToNext()) {
            image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
            //Log.d(TAG, "image_uri "+image_uri);
        }
        if(cursor !=null)
            cursor.close();
Neeraj Kumar
  • 645
  • 1
  • 7
  • 15