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");
startManagingCursor(cursor);

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

The code for the list_entry.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:id="@+id/list_entry_base">
	<ImageView
		android:id="@+id/profile_pic"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<LinearLayout
		android:orientation="vertical"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingLeft="12dip" >
	<TextView
		android:id="@+id/name_entry"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content" />
	<TextView
		android:id="@+id/number_entry"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	</LinearLayout>
</LinearLayout>

And finally the code for the SimpleContactAdapter (which is our custom cursor adapter – for more info see http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/) should look like:

@Override
    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(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }

        // set the profile picture
        ImageView profile = (ImageView) v.findViewById(R.id.profile_pic);
        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
            profile.setImageBitmap(bitmap);
        }

        // set the contact phone number
        TextView number_text = (TextView) v.findViewById(R.id.number_entry);
        if (number_text != null) {
            number_text.setText(number);
        }

        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

About these ads
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.

    Thanks!

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

    Hi,

    Can i get the fully source code for this example ? if yes, please send it to e_zy_clie@yahoo.com

    Thanks

  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??

    regards,
    mike

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

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

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

    Hi,
    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

Trackbacks

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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 891 other followers

%d bloggers like this: