Retrieving Contact Information (Name, Number, and Profile Picture)
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

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 >.<
Nice inspiring blog, dude. I enjoy your posting. I am about to create blog about mobile phones, too.
Thanks!
Hi,
Can i get the fully source code for this example ? if yes, please send it to e_zy_clie@yahoo.com
Thanks
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..
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
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
Hey,
nice example. this is working fine but fetching contact image only for first contact.
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
If I turn the device, which causes a onCreate(), the correct images are shown for the current view.
cheers man, just what i was looking for, most info is for later API versions
Your example works very fine in emulator but it is displaying empty screen in real device
Only images of the first two contacts were displayed… what to do