10

I am working with Android Contact ContentProvider. I have a Phone Number and I need to get the URI of the Photo of the contact associated with this phone number. How can I do it???

I know I can get the raw data of the photo and build an InputStream, but I dont want the input stream, I need the URI.

EDIT: Originally I'm using following code to fetch contact info

    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo));
    Cursor cursor = context.getContentResolver().query(uri, details, null, null, null);
coreSOLO
  • 3,599
  • 4
  • 20
  • 22

5 Answers5

12

To get the conatct id using the phone number use the following code:

import android.provider.ContactsContract.PhoneLookup;

public String fetchContactIdFromPhoneNumber(String phoneNumber) {
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
        Uri.encode(phoneNumber));
    Cursor cursor = this.getContentResolver().query(uri,
        new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
        null, null, null);

    String contactId = "";

    if (cursor.moveToFirst()) {
        do {
        contactId = cursor.getString(cursor
            .getColumnIndex(PhoneLookup._ID));
        } while (cursor.moveToNext());
    }

    return contactId;
  }

and use the contact id obtained to get the contatc photo URI. Use the following code for getting photo URI:

import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public Uri getPhotoUri(long contactId) {
    ContentResolver contentResolver = getContentResolver();

    try {
        Cursor cursor = contentResolver
            .query(ContactsContract.Data.CONTENT_URI,
                null,
                ContactsContract.Data.CONTACT_ID
                    + "="
                    + contactId
                    + " AND "

                    + ContactsContract.Data.MIMETYPE
                    + "='"
                    + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
                    + "'", null, null);

        if (cursor != null) {
        if (!cursor.moveToFirst()) {
            return null; // no photo
        }
        } else {
        return null; // error in cursor process
        }

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    Uri person = ContentUris.withAppendedId(
        ContactsContract.Contacts.CONTENT_URI, contactId);
    return Uri.withAppendedPath(person,
        ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
  }

Hope this would help.

Kannan Suresh
  • 4,573
  • 3
  • 34
  • 59
  • 4
    Don't forget to close the cursor when you're done or you'll get a memory leak. – pm_labs Apr 27 '13 at 10:32
  • 2
    Also be sure to include the READ_CONTACTS permission in your app's Manifest. – PeteH Jun 13 '13 at 23:32
  • 6
    This getPhotoUri proc is totally wrong in every possible way... DO NOT USE IT... You need to check the existance of the uri too.. Answer should be deleted.. If you need the uri read the PHOTO_URI or PHOTO_THUMBNAIL_URI from a content provider for valid URIs! – xnagyg Jan 07 '15 at 22:08
  • 1
    @xnagyg It's very easy to tell what not to do and what should be done without any example. Please provide your solution. – Udi Oshi Jun 20 '17 at 14:51
9

This solution demonstrates how to get an image from a user contact and then display it in an ImageView.

ImageView profile  = (ImageView)findViewById(R.id.imageView1);                 
Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(Contact_Id));
InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),my_contact_Uri);            
BufferedInputStream buf =new BufferedInputStream(photo_stream);
Bitmap my_btmp = BitmapFactory.decodeStream(buf);
profile.setImageBitmap(my_btmp);
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81
  • The code is missing "buf" in BufferedInputStream buf =new BufferedInputStream(photo_stream); – pm_labs Apr 27 '13 at 10:01
  • 4
    Also, you can directly set the Uri to an imageView (using imageView.setImageURI(uri) instead of having to convert to Bitmap yourself. – pm_labs Apr 27 '13 at 10:50
  • @paul_sns how about storing it into database? can i use the uri or the bitmap link? – ajdeguzman Feb 25 '14 at 02:03
5

Here's the code from Android Documentation.

Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
vida
  • 4,459
  • 1
  • 22
  • 27
2

You can get PHOTO_URI by NUMBER just use following code also you can use _ID.

 public static String getContactPhoto(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.PHOTO_URI}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactImage= null;
    if (cursor.moveToFirst()) {
        contactImage= cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
    }

    if (!cursor.isClosed()) {
        cursor.close();
    }
    return contactImage;
}
Sagar
  • 5,273
  • 4
  • 37
  • 50
0
    final boolean IS_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    String phoneNumber = "+1 416 385 7805";
    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.LOOKUP_KEY,
            IS_HONEYCOMB ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts._ID,
            };
    Cursor cursor =
            contentResolver.query(
                    uri,
                    projection,
                    null,
                    null,
                    null);
    if (cursor != null && cursor.moveToNext()) {
        long contactId = cursor.getLong(0);
        String lookupKey = cursor.getString(1);
        String thumbnailUri = cursor.getString(2);           
        cursor.close();
    }

So now if sdk is honeycomb or higher u have thumbnail uri of the contact. Or you can construct a lookup uri like this:

Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey);

P.S. If you already know contact id and/or lookup key you can construct a Uri from string:

lookup: content://com.android.contacts/contacts/lookup/{lookup key}/{contact id} thumbnail: content://com.android.contacts/contacts/{contact id}/photo

So it's better to cache these values.

Mussa
  • 1,463
  • 21
  • 25