Skip to content

Retrieving Contact Information (Name, Number, and Profile Picture)

January 19, 2010

Hey everyone,

This post is for API levels 4 or lower and is a post to honor someone’s request. The example will show you how to grab a person’s name, number, and profile picture, and set them in a view.

The code in the Activity is:

//query for the people in your address book
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,People.NAME + " ASC");

//bind the name and the number fields
String[] columns = new String[] { People.NAME, People.NUMBER };
int[] to = new int[] {, };
SimpleContactAdapter mAdapter = new SimpleContactAdapter(this, R.layout.list_entry, cursor, columns, to);

The code for the list_entry.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
		android:paddingLeft="12dip" >
		android:layout_height="wrap_content" />

And finally the code for the SimpleContactAdapter (which is our custom cursor adapter – for more info see should look like:

    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        Cursor c = getCursor();

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        int idCol = c.getColumnIndex(People._ID);
        int nameCol = c.getColumnIndex(People.NAME);
        int numCol = c.getColumnIndex(People.NUMBER);

        String name = c.getString(nameCol);
        String number = c.getString(numCol);
        long id = c.getLong(idCol);

        // set the name here
        TextView name_text = (TextView) v.findViewById(;
        if (name_text != null) {

        // set the profile picture
        ImageView profile = (ImageView) v.findViewById(;
        if (profile != null) {
            // retrieve the contact photo as a Bitmap
            Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
            Bitmap bitmap = People.loadContactPhoto(context, uri, R.drawable.icon, null);

            // set it here in the ImageView

        // set the contact phone number
        TextView number_text = (TextView) v.findViewById(;
        if (number_text != null) {

        return v;

And putting all those together should do it! Now you should be able to recreate your phone’s address book and put the name, number, and also the contact’s local photo.

Let me know if you have questions! Happy coding.

– jwei

15 Comments leave one →
  1. Carl permalink
    January 22, 2010 12:46 pm

    Hey man, really like the stuff you have on your blog, pretty much all of it matches my course projects atm, so it will come in rather helpful ^^ hehe, especially with Android, that stuff is hard >.<

  2. January 24, 2010 10:18 pm

    Nice inspiring blog, dude. I enjoy your posting. I am about to create blog about mobile phones, too.


  3. ezyclie permalink
    January 31, 2010 7:06 pm


    Can i get the fully source code for this example ? if yes, please send it to


  4. February 14, 2010 10:09 pm

    hi, this is really a helpful blog.
    but i would like to request to post snapshots of codes here…
    so that viewers can easily understand the code,,
    and that will help you also..

    • February 14, 2010 10:12 pm

      hey pendroid

      do u mean to put up snap shots of the emulator? and do u mean for this particular post, or in general?

      thanks for the feedback!

      – jwei

  5. Mike permalink
    April 21, 2010 7:31 pm

    hi jwei512,

    this is cool. this helps me a lot. but have a small issue, if user has a mobile number as well as a HOME or a WORK number too how to retrieve them and how to display them??


  6. Sulabh permalink
    October 7, 2010 12:01 am

    nice example. this is working fine but fetching contact image only for first contact.

  7. Beachsnow permalink
    November 9, 2010 12:15 pm

    I have the same problem as Sulabh.
    There must be a bug in the code (which I have not jet found) because I the image is only correct for the first two contacts and just repates this picture for the next contacts that have a image. Please help to fix it!

    Regards Stefan

    • Beachsnow permalink
      November 9, 2010 12:39 pm

      If I turn the device, which causes a onCreate(), the correct images are shown for the current view.

  8. March 21, 2011 6:09 am

    cheers man, just what i was looking for, most info is for later API versions

  9. satya permalink
    March 22, 2011 7:08 am

    Your example works very fine in emulator but it is displaying empty screen in real device

  10. Baradwaj permalink
    December 20, 2012 10:31 am

    Only images of the first two contacts were displayed… what to do


  1. A lot less Sauce
  2. Tweets that mention Retrieving Contact Information (Name, Number, and Profile Picture) « Think Android --
  3. PN.Truong Phuc's Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: