Query contacts Thumbnail from contact number


Ask by : nightcrawler23 November 06, 2012 13:52

i am trying to retreive the contacts thumbnail from phonebook given a phone number or email ID. Th code that i wrote works but takes a very long time because it is querying the whole contacts(not efficient at all).

Below is the code

Cursor cursor;
String[] queryColumns = { ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,ContactsContract.Contacts._ID };
ContentResolver cr = getContentResolver();
cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, queryColumns,
                            null, null, null);

cursor.moveToFirst();
while (cursor.moveToNext() && continueSearch) {
    String[] phonesAndEmails = extractPhonesAndEmails(Integer.parseInt(cursor.getString(1)));
    for(int g=0;g<phonesAndEmails.length;g++){
        if(phonesAndEmails[g].equals(searchFor)){
            contactThumbUri = cursor.getString(0);
            MyUtils.addLog("Found Match **************" + contactThumbUri);
            continueSearch=false;
        }
    }
}
cursor.close();

extractPhonesAndEmails gets a String[] of all the numbers and emails for the contactID passed to it.

I got another approach from SO that uses PhoneLookup but that giver=s me an error. I checke to see what columns is this cursor returning. i got only two columns one of which as contact name and the other was an integer probably the ID. Here is the second code;

if(!MyUtils.checkIfEmailID(searchFor)){
    Cursor mCursor;
    Uri qUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(searchFor));
    String[] mqueryColumns = {PhoneLookup.PHOTO_URI};
    mCursor = getContentResolver().query(qUri, mqueryColumns,null, null, null);
    MyUtils.addLog(mCursor.getCount() + " count");
    MyUtils.addLog(mCursor.getString(0));
    mCursor.moveToFirst();
    while (mCursor.moveToNext() && continueSearch) {
        contactThumbUri=mCursor.getString(0);
        MyUtils.addLog(contactThumbUri);
        continueSearch=false;
    }
    mCursor.close();    
                    }

please help to either optimize my first code or sort out the error in the second code.Thanks a lot.

View original question

Answer by : nightcrawler23November 05, 2012 14:07

With the help of Joe's answer and this post, i was able to come up with the code that worked perfectly for me. this code basically takes a phone number as input and returns the thumbnail uri to the contact if existing in phone. Here is the code.

Thanks Joe.

 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(searchFor));
String[] projection = {PhoneLookup.LOOKUP_KEY};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
do{
    String lookUpKey = cursor.getString(0);

    uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, Uri.encode(lookUpKey));
    String[] projection1 = {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
    Cursor cursor1 = getContentResolver().query(uri, projection1, null, null, null);
    cursor1.moveToFirst();
    contactThumbUri = cursor1.getString(0);
    cursor1.close();
    if(!contactThumbUri.equals(null)){
        continueSearch=false;
    }
}while(cursor.moveToNext() && continueSearch);
cursor.close();

a similar approach can be used for looking up emails. Need to use ContactsContract.Contacts.CONTENT_LOOKUP_URI instead of PhoneLookup.CONTENT_FILTER_URI

Hope this also helps other beginners like me :P

View original answer