Note that there are some explanatory texts on larger screens.

plurals
  1. POQuery contacts Thumbnail from contact number
    text
    copied!<p>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).</p> <p>Below is the code</p> <pre><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() &amp;&amp; continueSearch) { String[] phonesAndEmails = extractPhonesAndEmails(Integer.parseInt(cursor.getString(1))); for(int g=0;g&lt;phonesAndEmails.length;g++){ if(phonesAndEmails[g].equals(searchFor)){ contactThumbUri = cursor.getString(0); MyUtils.addLog("Found Match **************" + contactThumbUri); continueSearch=false; } } } cursor.close(); </code></pre> <p>extractPhonesAndEmails gets a String[] of all the numbers and emails for the contactID passed to it.</p> <p>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;</p> <pre><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() &amp;&amp; continueSearch) { contactThumbUri=mCursor.getString(0); MyUtils.addLog(contactThumbUri); continueSearch=false; } mCursor.close(); } </code></pre> <p>please help to either optimize my first code or sort out the error in the second code.Thanks a lot.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload