2

I'm trying to get email from ContactsContract.CommonDataKinds.Email.ADDRESS and ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.DATA1, all of these do not return email of my contact, which contains an email. I usually get phone number of said contact.

Everything I've tried has failed so far..

any ideas?

EDIT: What I've tried so far..

Uri contactData = data.getData();


                    String[] projection =
                            {
                                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    ContactsContract.CommonDataKinds.Email.ADDRESS 

                            };

                    Cursor cursor = getContentResolver().query(contactData,projection,null,null,null);
                    if(cursor.moveToFirst()){
                        String phoneName = cursor.getString(2);

                        Log.d("EMAIL:",phoneName);
                    }

For ContactsContract.CommonDataKinds.Email.ADDRESS,I have tried replacing ADDRESS with .DATA1, DATA, _ID, CONTACT_ID, etc.. NONE OF THESE RETURN EMAIL address, however, some return the phone number of the contact, but that's it.

Space Ghost
  • 765
  • 2
  • 13
  • 26
  • sure, I will edit my original post and add the code. – Space Ghost Apr 22 '15 at 06:20
  • thanks @spaceghost. I will upvote if your question meet the standards – madLokesh Apr 22 '15 at 06:21
  • can yo debug the code and check what are the values you are getting in cursor? – RobinHood Apr 22 '15 at 06:31
  • so you want both phone number and email address to be returned? – pskink Apr 22 '15 at 06:55
  • I'm asking legitimately here... does it matter? I do want both returned, but would my code have to be drastically different if I wanted just the phone number or just the email? – Space Ghost Apr 22 '15 at 06:57
  • have you noticed that both `ContactsContract.CommonDataKinds.Phone.NUMBER` and `ContactsContract.CommonDataKinds.Email.ADDRESS` point to `DATA1` ? – pskink Apr 22 '15 at 07:07
  • so if they both point to the **same** column they cannot be read from one Cursor row, for more info read ContactsContract.Data docs and http://developer.android.com/guide/topics/providers/contacts-provider.html – pskink Apr 22 '15 at 07:26

3 Answers3

2

Not sure whats wrong with your code, can you just execute below code in order to get email address of particular contact.

Sample Code:

 public String retrieveMailId(Context ctx, Uri contactUri) {
            String email = null, contactId = null;

            Cursor cursorID = ctx.getContentResolver().query(contactUri,
                    new String[] { ContactsContract.Contacts._ID }, null, null,
                    null);

            if (cursorID == null) {
                return null;
            }

            if (cursorID.moveToFirst()) {

                contactId = cursorID.getString(cursorID
                        .getColumnIndex(ContactsContract.Contacts._ID));
            }

            cursorID.close();

            if (TextUtils.isEmpty(contactId)) {
                return null;
            }

            // Using the contact ID now we will get email address.
            Cursor cursor = ctx.getContentResolver().query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
                            + contactId, null, null);

            if (cursor.moveToFirst()) {
                int colIdx = cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
                email = cursor.getString(colIdx);
            }
            cursor.close();

            return email;

        }
RobinHood
  • 10,897
  • 4
  • 48
  • 97
  • Thank you robinhood, I have applied your method perfectly, however, email returns a null. Please be aware, my contact that I am picking DOES have an email. – Space Ghost Apr 22 '15 at 06:49
  • If your contact doesn't contain email address then it will return null else you will get the email address. – RobinHood Apr 22 '15 at 07:04
  • I know. My contact contains 3 things. 1)the phone number 2)the phone number (with a +) in front of it. 3)an email address "abc@yahoo.com" – Space Ghost Apr 22 '15 at 07:04
  • Hmm.. I picked a different contact with an email. The result I got was support@unroll.me Any idea what this is? – Space Ghost Apr 22 '15 at 07:10
  • check manually which email address is associate with that contact in contact app, does it same or differ? – RobinHood Apr 22 '15 at 07:12
  • The same thing happens to me, I can't read the email from some contacts but it works for others. – soger Mar 30 '17 at 13:23
2

I'd just like to point out a small flaw when it comes to retrieving the ID of your contact: don't use ContactsContract.Contacts._ID, but instead use ContactsContract.CommonDataKinds.Email.CONTACT_ID in the string-array inside your query. Once you extract the ID use the following line of code:

String id = cursor.getString(
    cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.CONTACT_ID)
);
Jon
  • 9,156
  • 9
  • 56
  • 73
1

In Shortly , you need another cursor for the Email:

Cursor emails = contentResolver.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + contactId, null, null);
        while (emails.moveToNext()) 
        { 
           String email = emails.getString(emails.getColumnIndex(Email.DATA));
            break;
        }
        emails.close();

Hope it will help someone...

Ester Kaufman
  • 708
  • 10
  • 20