Organizational Research By

Surprising Reserch Topic

Question:Can't get the email address from ContactsContract


I'm trying to get the email address from the ContactsContract, but I only get an empty string! Name and number is working fine, but not the email address!

I confused and have been trying to solve this for days now, but I don't know what's wrong, if I have missed something in the code or what and I don't know how to solve this.

This part of the application has a listview of all names from the ContactsContract. When selecting a name in the listview, the code should get the name, number and email adress from the ContactsContract.

I would preciate some help to be able to continue the work! Perhaps there is a better way to solve this, then please tell me how to change! Thanks!

Since toast message dosen't show up inside the while loop, while(cursorEmail.moveToFirst()){.... I guess there is something wrong with the emails cursor!? It seems like it's empty!?

public class Activity_3 extends Activity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_3);
    listView = (ListView) findViewById(R.id.contactList);

    String[] projection = { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone._ID };

    Cursor cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);

    // From column
    String[] fromColumn = { ContactsContract.Contacts.DISPLAY_NAME };
    // To view
    int[] toView = { R.id.contactItem };

    startManagingCursor(cursor1);

    ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor1, fromColumn, toView);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView adapter, View view, int position, long id) {

            String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                    };

            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);

            cursor.moveToPosition(position);

            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String contactNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            String pos = Integer.toString(position);

            String contactEmailAddress = "?";   
            //Email
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId, null,  null);

            while(emails.moveToNext()){
                contactEmailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Toast.makeText(Activity_3.this, contactEmailAddress, Toast.LENGTH_SHORT).show();
            }
            emails.close();

            Toast.makeText(Activity_3.this, pos + " " + contactId + " " + contactName + " " + contactNumber + " " + contactEmailAddress, Toast.LENGTH_SHORT).show();
        }
    });
}

}

asked Sep 13, 2013 in Java Interview Questions by rajesh
edited Sep 12, 2013
0 votes
432 views



Related Hot Questions

10 Answers

0 votes
Assuming you've got the id for the specific contact you're about to get the email address for, here is a snippet of code i made a while back when i tried to do the same as you do now:

First you'll want to get the id for the contact:

    ContentResolver cr  = context.getContentResolver();
    Cursor cursor       = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    id                  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Then you can fetch the email address by doing this:

            String contactAddress   = "";
            Cursor cursorEmail      = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                            new String[] {id},
                                            null);
            if(cursorEmail.moveToFirst()) {
                contactAddress = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

Remember that a contact does not necessarily have an email, so you should ofcourse take that into account :-)

Edit: Took a better look at your code and compared it to what i've pasted here. It seems to be that your problem lies in the paramters selection and selectionArgs. What you want to do is get the id of the contact you're getting the email address for and then change your selection to:

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"

and add the id in the selectionArgs like this:
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
Here I am posting a working method which will return you the mail id or null(if not available) by a contact id.

public static String getEmail(Context c, long id) {
        Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Email._ID,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID,
                ContactsContract.CommonDataKinds.Email.ADDRESS,
                ContactsContract.CommonDataKinds.Email.TYPE };
        String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID
                + " = '" + id + "'";
        String sortOrder = ContactsContract.CommonDataKinds.Email.ADDRESS
                + " COLLATE LOCALIZED ASC";

        Cursor cursor = c.getContentResolver().query(uri, projection,
                selection, null, sortOrder);

        int index = cursor
                .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
        if (cursor.moveToNext()) {
            return cursor.getString(index);
        }
        return null;
    }
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
String email = "";  
            try {  
                Uri result = data.getData();  
                Log.v(DEBUG_TAG, "Got a contact result: "  
                        + result.toString());  
                // get the contact id from the Uri  
                String id = result.getLastPathSegment();  
                // query for everything email  
                cursor = getContentResolver().query(Email.CONTENT_URI,  
                        null, Email.CONTACT_ID + "=?", new String[] { id },  
                        null);  
                int emailIdx = cursor.getColumnIndex(Email.DATA);  
                // let's just get the first email  
                if (cursor.moveToFirst()) {  
                    email = cursor.getString(emailIdx);  
                    Log.v(DEBUG_TAG, "Got email: " + email);  
                } else {  
                    Log.w(DEBUG_TAG, "No results");  
                }  
            } catch (Exception e) {  
                Log.e(DEBUG_TAG, "Failed to get email data", e);  
            } finally {  
                if (cursor != null) {  
                    cursor.close();  
                } 

               edtCustomerEmailid.setText(email);

              //Toast.makeText(getApplicationContext(), email, 1).show();
                if (email.length() == 0) {  
                    Toast.makeText(this, "No email found for contact.",  
                            Toast.LENGTH_LONG).show();  
                }  
            } 
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
Assuming you've got the id for the specific contact you're about to get the email address for, here is a snippet of code i made a while back when i tried to do the same as you do now:

First you'll want to get the id for the contact:

    ContentResolver cr  = context.getContentResolver();
    Cursor cursor       = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    id                  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Then you can fetch the email address by doing this:

            String contactAddress   = "";
            Cursor cursorEmail      = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                            new String[] {id},
                                            null);
            if(cursorEmail.moveToFirst()) {
                contactAddress = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

Remember that a contact does not necessarily have an email, so you should ofcourse take that into account :-)

Edit: Took a better look at your code and compared it to what i've pasted here. It seems to be that your problem lies in the paramters selection and selectionArgs. What you want to do is get the id of the contact you're getting the email address for and then change your selection to:

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"0 down vote
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
I√Ę‚ā¨‚ĄĘm not that much of a online reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your website to come back later on. Many thanks torty katowice ,i,ekonomia/cukiernia,lukasz,szachta,torty,na,zamowienie,katowice,s,3318/
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013 by rajesh
0 votes
Does your website have a contact page? I'm having trouble locating it but, I'd like to send you an email. I've got some ideas for your blog you might be interested in hearing. Either way, great website and I look forward to seeing it grow over time. szko³a dla doros³ych warszawa
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013 by rajesh
0 votes
Here I am posting a working method which will return you the mail id or null(if not available) by a contact id.

public static String getEmail(Context c, long id) {
        Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Email._ID,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID,
                ContactsContract.CommonDataKinds.Email.ADDRESS,
                ContactsContract.CommonDataKinds.Email.TYPE };
        String selection = ContactsContract.CommonDataKinds.Email.CONTACT_ID
                + " = '" + id + "'";
        String sortOrder = ContactsContract.CommonDataKinds.Email.ADDRESS
                + " COLLATE LOCALIZED ASC";

        Cursor cursor = c.getContentResolver().query(uri, projection,
                selection, null, sortOrder);

        int index = cursor
                .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS);
        if (cursor.moveToNext()) {
            return cursor.getString(index);
        }
        return null;
    }
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
ContentResolver cr = this.getContext().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));



        }
        cur1.close();
    }
}
ContentResolver cr = this.getContext().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));



        }
        cur1.close();
    }
}
0 votes
String email = "";  
            try {  
                Uri result = data.getData();  
                Log.v(DEBUG_TAG, "Got a contact result: "  
                        + result.toString());  
                // get the contact id from the Uri  
                String id = result.getLastPathSegment();  
                // query for everything email  
                cursor = getContentResolver().query(Email.CONTENT_URI,  
                        null, Email.CONTACT_ID + "=?", new String[] { id },  
                        null);  
                int emailIdx = cursor.getColumnIndex(Email.DATA);  
                // let's just get the first email  
                if (cursor.moveToFirst()) {  
                    email = cursor.getString(emailIdx);  
                    Log.v(DEBUG_TAG, "Got email: " + email);  
                } else {  
                    Log.w(DEBUG_TAG, "No results");  
                }  
            } catch (Exception e) {  
                Log.e(DEBUG_TAG, "Failed to get email data", e);  
            } finally {  
                if (cursor != null) {  
                    cursor.close();  
                } 

               edtCustomerEmailid.setText(email);

              //Toast.makeText(getApplicationContext(), email, 1).show();
                if (email.length() == 0) {  
                    Toast.makeText(this, "No email found for contact.",  
                            Toast.LENGTH_LONG).show();  
                }  
            } 
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
Assuming you've got the id for the specific contact you're about to get the email address for, here is a snippet of code i made a while back when i tried to do the same as you do now:

First you'll want to get the id for the contact:

    ContentResolver cr  = context.getContentResolver();
    Cursor cursor       = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    id                  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Then you can fetch the email address by doing this:

            String contactAddress   = "";
            Cursor cursorEmail      = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                            new String[] {id},
                                            null);
            if(cursorEmail.moveToFirst()) {
                contactAddress = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

Remember that a contact does not necessarily have an email, so you should ofcourse take that into account :-)

Edit: Took a better look at your code and compared it to what i've pasted here. It seems to be that your problem lies in the paramters selection and selectionArgs. What you want to do is get the id of the contact you're getting the email address for and then change your selection to:

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"0 down vote
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013
0 votes
Assuming you've got the id for the specific contact you're about to get the email address for, here is a snippet of code i made a while back when i tried to do the same as you do now:

First you'll want to get the id for the contact:

    ContentResolver cr  = context.getContentResolver();
    Cursor cursor       = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    id                  = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Then you can fetch the email address by doing this:

            String contactAddress   = "";
            Cursor cursorEmail      = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                                            new String[] {id},
                                            null);
            if(cursorEmail.moveToFirst()) {
                contactAddress = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            }

Remember that a contact does not necessarily have an email, so you should ofcourse take that into account :-)

Edit: Took a better look at your code and compared it to what i've pasted here. It seems to be that your problem lies in the paramters selection and selectionArgs. What you want to do is get the id of the contact you're getting the email address for and then change your selection to:

ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?"

and add the id in the selectionArgs like this:
answered Sep 13, 2013 by rajesh
edited Sep 12, 2013

...