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.