0

I am working on an Android app.In my App I have to show the contact display name and displaypic. For getting contacts displayname by number I am using the following code.

 public String getContactDisplayNameByNumber(String number) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    String name = " ";

    ContentResolver contentResolver = getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
            ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));

            //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}

Its working fine .Please help me to get contact displaypic like this.

sarath
  • 3,181
  • 7
  • 36
  • 58
  • 1
    This is possibly duplicate http://stackoverflow.com/a/8695649/704374 – Akshay Aug 14 '12 at 10:40
  • @Akshay..You are right..But that question only for displayname..Thats why I didn't check that. Thanks for the help. If you post it as an answer then I could accept.:) – sarath Aug 14 '12 at 11:16

1 Answers1

1

The following code is worked for me.

public Drawable getContactDisplaypicByNumber(String number) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    String contactId = null;
    InputStream input = null;
    String[] projection = new String[] {
            ContactsContract.PhoneLookup.DISPLAY_NAME,
            ContactsContract.PhoneLookup._ID};
        Drawable d=null;

    ContentResolver contentResolver = getContentResolver();
    Cursor contactLookup = contentResolver.query(uri,projection, null, null, null);


    if (contactLookup.moveToFirst()) {
        contactId = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.PhoneLookup._ID));

        Uri uri1 = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
        input = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver, uri1);


    }
    else {

        Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number);

        return d; // contact not found

    }

    if (input == null) {
        Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name);
        d=getResources().getDrawable(R.drawable.ic_launcher);
        return d; // no photo
    } else {

        Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name);
        d=Drawable.createFromStream(input, "");
        System.out.println("ddddddddddddddddddddddddddddddddd"+d);
        return d;
    }

}
sarath
  • 3,181
  • 7
  • 36
  • 58