Retrieve only email address from Android contact list

I want to display only those contact names whose email address is present. Otherwise, this contact name should not be displayed in the list. How can i do this? Can anyone help me out?

+53
android
Apr 12 2018-12-12T00:
source share
7 answers
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 returns an arraylist of names that have an email id.

+68
Apr 12 2018-12-12T00:
source share
β€” -

Here is my super quick email request. This is much faster than pulling all contact columns as other answers suggest ...

 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; } 

I tried the code provided by Agarwal Shankar, but it took about 4 seconds to get the contacts on my test device, and this code took about 0.04 seconds.

+116
Apr 05 '13 at 4:32
source share

Another solution.

 private static final Uri URI_CONTACT_DATA = ContactsContract.Data.CONTENT_URI; private static final String COLUMN_EMAIL = ContactsContract.CommonDataKinds.Email.ADDRESS; private static final String COLUMN_DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY; private static final String COLUMN_MIMETYPE = ContactsContract.Data.MIMETYPE; private static final String[] PROJECTION = { COLUMN_DISPLAY_NAME, COLUMN_EMAIL, COLUMN_MIMETYPE }; private Cursor getCursor() { ContentResolver resolver = context.getContentResolver(); String selection = COLUMN_MIMETYPE + "=?"; final String[] selectionArgs = {ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE}; return resolver.query(URI_CONTACT_DATA, PROJECTION, selection, selectionArgs, null); } 

The problem is that the ContactsContract.Contacts.CONTENT_URI table contains the entire contacts database . This includes phone numbers, emails, organizations, and even fully customizable data, so you cannot use it without filtering with ContactsContract.Data.MIMETYPE . A row in this database contains a value (or values, it contains 15 common columns) associated with a particular account, so you may need to group them yourself. I needed this to autocomplete emails, so the format (email in line) was perfect.

+2
Mar 18 '16 at 9:24
source share
 if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id},null); while (emailCur.moveToNext()) { String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); Log.e("Email",name+" "+email); } emailCur.close(); } } 
+1
Apr 12 2018-12-12T00:
source share

If you can already get contacts and their email address (if one exists), why don't you just delete contacts without an email address from your list?

See here for more information on the Android Contacts API.

0
Apr 12 2018-12-12T00:
source share

Declare a global variable

 // Hash Maps Map<String, String> nameEmailMap = new HashMap<String, String>(); 

Then use the function below

 private void getEmailIDs() { Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null); // Loop Through All The Emails while (emails.moveToNext()) { String name = emails.getString(emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)); // Enter Into Hash Map nameEmailMap.put(email, name); } // Get The Contents of Hash Map in Log for (Map.Entry<String, String> entry : nameEmailMap.entrySet()) { String key = entry.getKey(); Log.d(TAG, "Email :" + key); String value = entry.getValue(); Log.d(TAG, "Name :" + value); } emails.close(); } 

Remember that in the example above, the key is the email address and the value is the name, so read the contents, for example, mahaXXXX @gmail. com-> Mahatma Gandhi instead of Mahatma Gandhi--> mahaXXXX@gmail.com

0
Jan 17 '19 at 0:13
source share

Here is an easy way to get the email contact id from your contact list. You need to pass the user’s contact id below and it will return you the email id if exists

  public String getEmail(String contactId) { String emailStr = ""; final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE}; Cursor emailq = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[]{contactId}, null); if (emailq.moveToFirst()) { final int contactEmailColumnIndex = emailq.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA); while (!emailq.isAfterLast()) { emailStr = emailStr + emailq.getString(contactEmailColumnIndex) + ";"; emailq.moveToNext(); } } return emailStr; } 

And also, if you want to learn how to get the contact list in your application, follow this link: show the contact list in the android application - trinitytuts

-one
Jan 22 '16 at 5:33
source share



All Articles