How to fetch Contact list in android to use it in own way. The android OS provides feature to fetch all contacts from android device like Name, Image, Phone No., Email Id. It fetches all contacts from all added accounts like Facebook, Gmail, Watsapp and your android device which stores contacts. This tutorial is about to fetch contact list from an android device.
This example uses the ContactsContract and ContentResolver classes to fetch all contacts from android device. The URI used is CONTENT_URI to fetch contacts.
The above given code will help to fetch all contacts from android device.
This example uses the ContactsContract and ContentResolver classes to fetch all contacts from android device. The URI used is CONTENT_URI to fetch contacts.
ContentResolver cr = this.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
image_uri = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer.parseInt( cur.getString( cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER ) ) ) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
pCur.close();
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null);
int e = 0;
while (emailCur.moveToNext()) {
emailContact = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
}
}
The above given code will help to fetch all contacts from android device.