Fetch all Contacts from Contacts Content Provider in Android

Here I am going to explain how to fetch all contacts from Contacts content provider. As you already know content provider is used to share data between applications. Here we will get the data from contacts content provider to our application. First we have to create an object of ContentResolver.

 ContentResolver contentResolver = getContentResolver()  

Then query the content resolver using the Contacts URI.

 Cursor contactsCursor = resolver.query(Contacts.CONTENT_URI, null, null, null, null);  

After querying we will get the contacts ID. Using contacts ID, we can get the phone number,

 String id = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts._ID));  
 // to get phone number of respective contact  
 Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] { id }, null);  
 String phoneNumber = "";  
 if (phoneCursor.moveToFirst()) {  
      phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));  

Then to get the contacts name, contacts photo and email, use the below code

 String name = contactsCursor.getString(contactsCursor.getColumnIndex(Contacts.DISPLAY_NAME));  
 String photoUri = "";  
 photoUri = contactsCursor.getString(contactsCursor.getColumnIndex(Photo.PHOTO_URI));  
 // to get email id of respective contact  
 Cursor emailCursor = resolver.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = ?", new String[] { id }, null);  
 String email = "";  
 if (emailCursor.moveToFirst()) {  
      email = emailCursor.getString(emailCursor.getColumnIndex(Email.ADDRESS));  

Here some tips for SQLITE query. To limit the records, in query() API for the last parameter first sort for an attribute, then limit the records using limit ,

Eg: Contacts._ID DESC LIMIT 10,10

Here contacts will be sorted in descending order by _ID and we will get 10 records from 10 to 20.

The new query looks like this,

 Cursor contactsCursor = resolver.query(Contacts.CONTENT_URI, null, null, null, Contacts._ID+" DESC LIMIT 10,10");  

We can get the full source code by clcicking HERE

The screenshots are here as follows,

Also by clicking on the selected contact, it will call to that particular contact, if it has a phone number.


Popular posts from this blog

Open Gallery and get the selected image in appplication

Dismiss or Cancel dialog by swipe gesture

AlarmManager and Notification in Android