0

I use this method to get email addresses:

ContentResolver cr = SelectFile.this.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()) {
                        //to get the contact names
                        String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        ItemEmailSuggestion item = new ItemEmailSuggestion(email,name,id);
                        emailsContacts.add(item);
                        Log.i("CONTACTS",email+" "+name);
                    }
                    cur1.close();
                }
            }

(source Get only email address from contact list Android)

in order to propose a list of mail according to the input in an editText.

My implementation works, but the list of mail addresses obtained is much less provided compared to the mails registered in the Gmail application.

Why?

Of course permissions are granted correctly (READ_CONTACT).

I have unfortunately no more terminal under kitkat, but it seems to remember that this was not the case and that the list was comparable to that of Gmail.

Currently tested under android 21,22,23,24.

thank you in advance

Aristide13
  • 244
  • 2
  • 16

1 Answers1

0

Not all emails stored on the device belong to real contacts with names, you can try querying for all emails directly from the Emails table, and if that works, your can fill in names for those that have names in a second query.

        cr.query(Email.CONTENT_URI,null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
              String email = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
              Log.i("CONTACTS",email);
           }
           cur.close();
        }
marmor
  • 27,641
  • 11
  • 107
  • 150