0

I am trying to generate a list of emails from the phone's contacts but I have having trouble specifically getting emails. I can get names and phone numbers but not emails. The ArrayList sizes are both 0 at the end of the loop. Can anyone spot what I am doing wrong, or tell me how emails differ from phone numbers, names and other information?

    ContentResolver cr = getContentResolver();

    Cursor emailCur = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
            null, null);

    ArrayList<String> emails = new ArrayList<String>();
    ArrayList<String> emailTypes = new ArrayList<String>();

    while (emailCur.moveToNext()) {
        // This would allow you get several email addresses
        // if the email addresses were stored in an array
        String email = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String emailType = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

        emails.add(email);
        emailTypes.add(emailType);

    }

    emailCur.close();

    Log.i("Emails Count", Integer.toString(emails.size()));
    for (int i = 0; i < emails.size(); i++) {
        Log.i("Emails " + i, emails.get(i));
    }

    Log.i("EmailTypes Count", Integer.toString(emailTypes.size()));
    for (int i = 0; i < emailTypes.size(); i++) {
        Log.i("EmailTypes " + i, emailTypes.get(i));
    }
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
NotACleverMan
  • 12,107
  • 12
  • 47
  • 67

1 Answers1

2

use this code to get email from device.

ContentResolver cr = getContentResolver();
                 cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);

                emailIndex=0;
                if (cur.getCount() > 0) 
                {

                    while (cur.moveToNext()) 
                    {
                        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                        name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                        Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + id, null, null);
                        while (emails.moveToNext()) 
                        {
                            // This would allow you get several email addresses
                            String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                            Log.v(name+"==>", emailAddress);

                        }
                        emails.close();         
                    }
                    cur.close();
Kakey
  • 3,936
  • 5
  • 29
  • 45