1

I am trying to fetch only email contacts that are available in my contacts. Right now I have got a solution that shows all the contacts and if selected contacts doesn't have an email address it would toast stating no email address found. Instead I would like to show contacts that has only email address.

Here is the query that I tried:

Cursor cursor = null;  
            String emailid = "";
            List<String> allids = new ArrayList<String>();
            int emailIds = 0;
            try 
            {  

                Uri result = data.getData();  
                String id = result.getLastPathSegment();  
                Log.e("Email","TRY"+emailid);
                cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);                   
                /*cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
                new String[]{id}, null);*/
                emailIds = cursor.getColumnIndex(Email.DATA);
                if (cursor.moveToFirst()) 
                {
                    while (cursor.isAfterLast() == false)
                    {
                        emailid = cursor.getString(emailIdx);
                        allids.add(emailid);
                        cursor.moveToNext();
                    }
                } 

                else 
                {
                    //no results actions

                }  
            }

Can somebody let me know how to get email query part working?

Thanks!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144
  • See [Get only email address from contact list Android](http://stackoverflow.com/questions/10117049/get-only-email-address-from-contact-list-android) – neo108 Jun 12 '13 at 02:43
  • Nope. I tried it before. This is doesn't display only contacts which has email address. When I open contacts I want it show only contacts with email address. In the above link it shows everything and then gets it filtered in my program for which I already have a solution. – TheDevMan Jun 12 '13 at 03:49

1 Answers1

5

2 Algorithm that fetches contact with email

public ArrayList<String> getNameEmailDetails(){
        ArrayList<String> names = new ArrayList<String>();
        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()) { 
                    //to get the contact names
                    String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    Log.e("Name :", name);
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                    if(email!=null){
                        names.add(name);
                    }
                } 
                cur1.close();
            }
        }
        return names;
    }

the above method return an arraylist of names which has email id. But is slow, here is an another algorithm, much faster:

public ArrayList<String> getNameEmailDetails() {
    ArrayList<String> emlRecs = new ArrayList<String>();
    HashSet<String> emlRecsHS = new HashSet<String>();
    Context context = getActivity();
    ContentResolver cr = context.getContentResolver();
    String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID, 
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_ID,
            ContactsContract.CommonDataKinds.Email.DATA, 
            ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
    String order = "CASE WHEN " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + " NOT LIKE '%@%' THEN 1 ELSE 2 END, " 
            + ContactsContract.Contacts.DISPLAY_NAME 
            + ", " 
            + ContactsContract.CommonDataKinds.Email.DATA
            + " COLLATE NOCASE";
    String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
    Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
    if (cur.moveToFirst()) {
        do {
            // names comes in hand sometimes
            String name = cur.getString(1);
            String emlAddr = cur.getString(3);

            // keep unique only
            if (emlRecsHS.add(emlAddr.toLowerCase())) {
                emlRecs.add(emlAddr);
            }
        } while (cur.moveToNext());
    }

    cur.close();
    return emlRecs;
}

first code took about 4 seconds to get contacts on my test device, and this second code took about 0.04 sec.

fecub
  • 945
  • 10
  • 27
  • 1
    In above case you fetched only email. But i need email and phone number with only one Cursor do While.(Because i need to fetch 5000 contacts within 7 max. seconds) Please help me, I'm stuck in this from last few days. – Shrikant K Jan 09 '16 at 13:06