This is the complete solution
public ArrayList<HashMap<String, Object>> getContacts() {
ArrayList<HashMap<String, Object>> contacts = new ArrayList<HashMap<String, Object>>();
final String[] projection = new String[] { RawContacts.CONTACT_ID, RawContacts.DELETED };
@SuppressWarnings("deprecation")
final Cursor rawContacts = managedQuery(RawContacts.CONTENT_URI, projection, null, null, null);
final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);
final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);
if (rawContacts.moveToFirst()) {
while (!rawContacts.isAfterLast()) {
final int contactId = rawContacts.getInt(contactIdColumnIndex);
final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);
if (!deleted) {
HashMap<String, Object> contactInfo = new HashMap<String, Object>() {
{
put("contactId", "");
put("name", "");
put("email", "");
put("address", "");
put("photo", "");
put("phone", "");
}
};
contactInfo.put("contactId", "" + contactId);
contactInfo.put("name", getName(contactId));
contactInfo.put("email", getEmail(contactId));
contactInfo.put("photo", getPhoto(contactId) != null ? getPhoto(contactId) : "");
contactInfo.put("address", getAddress(contactId));
contactInfo.put("phone", getPhoneNumber(contactId));
contactInfo.put("isChecked", "false");
contacts.add(contactInfo);
}
rawContacts.moveToNext();
}
}
rawContacts.close();
return contacts;
}
private String getName(int contactId) {
String name = "";
final String[] projection = new String[] { Contacts.DISPLAY_NAME };
final Cursor contact = managedQuery(Contacts.CONTENT_URI, projection, Contacts._ID + "=?", new String[] { String.valueOf(contactId) }, null);
if (contact.moveToFirst()) {
name = contact.getString(contact.getColumnIndex(Contacts.DISPLAY_NAME));
contact.close();
}
contact.close();
return name;
}
private String getEmail(int contactId) {
String emailStr = "";
final String[] projection = new String[] { Email.DATA, // use
// Email.ADDRESS
// for API-Level
// 11+
Email.TYPE };
final Cursor email = managedQuery(Email.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);
if (email.moveToFirst()) {
final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);
while (!email.isAfterLast()) {
emailStr = emailStr + email.getString(contactEmailColumnIndex) + ";";
email.moveToNext();
}
}
email.close();
return emailStr;
}
private Bitmap getPhoto(int contactId) {
Bitmap photo = null;
final String[] projection = new String[] { Contacts.PHOTO_ID };
final Cursor contact = managedQuery(Contacts.CONTENT_URI, projection, Contacts._ID + "=?", new String[] { String.valueOf(contactId) }, null);
if (contact.moveToFirst()) {
final String photoId = contact.getString(contact.getColumnIndex(Contacts.PHOTO_ID));
if (photoId != null) {
photo = getBitmap(photoId);
} else {
photo = null;
}
}
contact.close();
return photo;
}
private Bitmap getBitmap(String photoId) {
final Cursor photo = managedQuery(Data.CONTENT_URI, new String[] { Photo.PHOTO }, Data._ID + "=?", new String[] { photoId }, null);
final Bitmap photoBitmap;
if (photo.moveToFirst()) {
byte[] photoBlob = photo.getBlob(photo.getColumnIndex(Photo.PHOTO));
photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
} else {
photoBitmap = null;
}
photo.close();
return photoBitmap;
}
private String getAddress(int contactId) {
String postalData = "";
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[] { String.valueOf(contactId), ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
Cursor addrCur = managedQuery(ContactsContract.Data.CONTENT_URI, null, addrWhere, addrWhereParams, null);
if (addrCur.moveToFirst()) {
postalData = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
addrCur.close();
return postalData;
}
private String getPhoneNumber(int contactId) {
String phoneNumber = "";
final String[] projection = new String[] { Phone.NUMBER, Phone.TYPE, };
final Cursor phone = managedQuery(Phone.CONTENT_URI, projection, Data.CONTACT_ID + "=?", new String[] { String.valueOf(contactId) }, null);
if (phone.moveToFirst()) {
final int contactNumberColumnIndex = phone.getColumnIndex(Phone.DATA);
while (!phone.isAfterLast()) {
phoneNumber = phoneNumber + phone.getString(contactNumberColumnIndex) + ";";
phone.moveToNext();
}
}
phone.close();
return phoneNumber;
}
How to use?
ArrayList<HashMap<String, Object>> contactList = getContacts();
System.out.println("Contact List : " +contactList);
Output:
[
{
phone=992-561-1618;848-807-4440;,
contactId=1,
photo=android.graphics.Bitmap@44f40aa0,
address=Zalavadia Strret
Manavadar, Gujarat 362630
India,
[email protected];[email protected];,
name=Biraj Zalavadia
},
{
phone=992-511-1418;842-827-4450;,
contactId=2,
photo=android.graphics.Bitmap@44f40aa0,
address=Makadiya Strret
Junagadh, Gujarat 364890
India,
[email protected];[email protected];,
name=Niles patel
}
.......
]
NOTE:
You will get phone and email semicolon(;) separated if its more than one.