Start:
I share two class. One is called Person class (Entity class)& other is ContactUitls which will give the all the contact list.
PersonClass
public class Person {
private String name;
private String phone_number;
private String lat;
private String lon;
private Uri image_uri;
private Bitmap image_in_bitmap = null;
private boolean selected = false;
public Person(String name, String phone_number, String lat, String lon,
Uri image_uri, Bitmap image_in_bitmap) {
super();
this.name = name;
this.phone_number = phone_number;
this.lat = lat;
this.lon = lon;
this.image_uri = image_uri;
this.image_in_bitmap = image_in_bitmap;
}
public Person() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return phone_number;
}
public void setNumber(String number) {
this.phone_number = number;
}
public Uri getImage_Uri() {
return image_uri;
}
public void setImage_Uri(Uri image_uri) {
this.image_uri = image_uri;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public Bitmap getImage_in_bitmap() {
return image_in_bitmap;
}
public void setImage_in_bitmap(Bitmap image_in_bitmap) {
this.image_in_bitmap = image_in_bitmap;
}
}
ContactUitls : to get the contact list. This class contains also several method to get contact list with photo, without photo, & photo URI
public class ContactUitls {
private static final String TAG = "PhoneUtils";
// good one.
public ArrayList<Person> getAllContactsWithPhoto(Context context) {
ArrayList<Person> allContacts = new ArrayList<Person>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
// if (Integer
// .parseInt(cur.getString(cur
// .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
// > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.Data.CONTACT_ID + " = ?",
new String[] { id }, null);
while (pCur.moveToNext()) {
// fill object data
Person aPerson = new Person();
aPerson.setName(pCur.getString(pCur
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
aPerson.setNumber(pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
// aPerson.setImage_Uri(getPhotoUri(context, id));
String test = null;
Uri my_contact_Uri = null;
test = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (test != null) {
my_contact_Uri = Uri.parse(test);
}
try {
if (my_contact_Uri != null) {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(
context.getContentResolver(),
my_contact_Uri);
aPerson.setImage_in_bitmap(bitmap);
} else {
// no need to set
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(" Number List",
" " + aPerson.getName() + " " + aPerson.getNumber()
+ " " + aPerson.getImage_Uri());
allContacts.add(aPerson);
}
pCur.close();
// }
}
}
return allContacts;
}
public ArrayList<Person> getAllContactsWithoutPhoto(Context context) {
ArrayList<Person> allContacts = new ArrayList<Person>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String ur_string = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.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()) {
Person aBlockNumber = new Person();
aBlockNumber
.setName(pCur.getString(pCur
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)));
aBlockNumber
.setNumber(pCur.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
// Log.d(" Number List", " " + aBlockNumber.getName()
// + " " + aBlockNumber.getNumber() + " "
// + aBlockNumber.getImage_uri());
allContacts.add(aBlockNumber);
}
pCur.close();
}
}
}
return allContacts;
}
public static Uri getPhotoUri(Context context, String id) {
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
Uri photo = Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor cur = context
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ id
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
cur.close();
return photo;
}
}