0

I am trying to read contacts from device. I am getting the list of device contacts , but the problem is most of them are duplicates. But in my default contacts app those duplicates are not there. Right now it is returning around 1150 contacts from device out of that around 602 are duplicates (when I checked the result using log), but my default contacts App is not showing this duplicates. This is my code

private void readPhoneContacts(Context context) {
    try {
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        HashMap<String, Contact> mContactListItems = new HashMap<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String key = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                String image_uri = "";
                String phoneNumber = "";
                String name = "";
                String emailId = null;
                int image_urlCursorIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI);
                if (!cursor.isNull(image_urlCursorIndex)) {
                    image_uri = cursor.getString(image_urlCursorIndex);
                }
                int phoneNumberCursorIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                if (!cursor.isNull(phoneNumberCursorIndex)) {
                    phoneNumber = cursor.getString(phoneNumberCursorIndex);
                }
                int nameCursorIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                if (!cursor.isNull(nameCursorIndex)) {
                    name = cursor.getString(nameCursorIndex);
                }
                emailId = cursor.getString(ContactsContract.CommonDataKinds.Email.TYPE_HOME);
                Contact contact = new Contact();
                contact.setName(name);
                contact.setEmailId(emailId);
                contact.setMobileNumber(phoneNumber);
                contact.setId(key);
                key = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                if (image_uri != null && !image_uri.isEmpty()) {
                    contact.setHasLocalContactImage(true);
                    contact.setDeviceContactImageUri(Uri.parse(image_uri));
                }
                mContactListItems.put(String.valueOf(key), contact);

            }
            cursor.close();


        }
    } catch (SQLiteException e) {

    } catch (SecurityException e) {

    } catch (Exception e) {

    }
}

What I want to get is Id , Name , Phone number , Email Id of all contacts from device. Also it should list all types of phone and email (home , office etc). Is there any way to avoid duplicates and get these data? Thanks in advance.

dev
  • 1,085
  • 4
  • 19
  • 26
  • you are using Phone.CONTENT_URI so you will not get email info as that uri doesnot store it – pskink Mar 09 '16 at 08:20
  • this link will helps you http://stackoverflow.com/questions/21471850/how-to-get-all-contacts-first-name-last-name-email-phone-number-etc-without?rq=1 – saeed Mar 09 '16 at 08:29

0 Answers0