-1

I am trying to fetch email and display name from contact list. My code is working fine till api level 4.4 (Kitkat) but not on 5.0 Lollipop or above (6.0 Marshmallow).

Here is my code :

public ArrayList<AddressData> getEmailDetails(){
        ArrayList<AddressData> alAdressBookData = new ArrayList<AddressData>();
        ContentResolver cr = 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));
                Cursor cur1 = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                new String[]{id}, null);
                while (cur1.moveToNext()) {
                    String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    AddressData obAddressData=new AddressData();
                    obAddressData.email=email;
                    obAddressData.name=name;
                    System.out.println("Contact Emails : "+ email);
                    System.out.println("Contact name : "+ name);
                    if(email!=null){
                        alAdressBookData.add(obAddressData);
                    }
                } 
                cur1.close();
            }
        }
        return alAdressBookData;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Harry
  • 369
  • 2
  • 17
  • what's not working?any exception? – Vivek_Neel May 18 '16 at 06:58
  • You should probably start by understanding the API and tables then use that to figure out how that data is stored and use logging to figure out WHAT exactly you are seeing (also look at the logs while it tries to get the data and see the system's response. Learn your APIs and debug your code instead of relying on the fact that it worked before. Also should start by reading the documentation as the permission structure for android M has changed, this may be your problem. You may have 2 problems, lollipop and M which the issues seen may NOT be because of the same reason. – JoxTraex May 18 '16 at 06:58

1 Answers1

4

This code works fine for me.

public static List<PhoneContact> getPhoneBook(Context context) {
    List<PhoneContact> result = new ArrayList<>();
    ContentResolver resolver = context.getContentResolver();
    Cursor contacts = null;
    try {
        contacts = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (contacts.moveToFirst()) {
            do {
                String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));
                PhoneContact phoneContact = new PhoneContact();
                Cursor emails = null;
                Cursor phones = null;
                try {
                    emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
                            + " = " + contactId, null, null);
                    while (emails.moveToNext()) {
                        String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        // Add email to your phoneContact object
                    }
                    phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                    while (phones.moveToNext()) {
                        String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String displayName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        // Add others information into your phoneContact object
                    }
                } finally {
                    if (emails != null) {
                        emails.close();
                    }
                    if (phones != null) {
                        phones.close();
                    }
                }
                result.add(phoneContact);
            } while (contacts.moveToNext());
        }
    } finally {
        if (contacts != null) {
            contacts.close();
        }
    }
    return result;
}

Need to add this permission into your manifest.

<uses-permission android:name="android.permission.READ_CONTACTS" />

Haven
  • 546
  • 5
  • 17