unresolved_external
unresolved_external

Reputation: 2018

Getting contact email by name

I've been trying to get email for contact by its name, but stuck with some difficultes. here is how I am trying to do this:

    Cursor emailCur = cr.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
            ContactsContract.CommonDataKinds.Email.DISPLAY_NAME + " = ?",
            new String[] { contactName }, null);
    while (emailCur.moveToNext()) {
        String email = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        String emailType = emailCur
                .getString(emailCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                }
    emailCur.close();

I constantly get an exception, what am I doing wrong?

Now I am getting zero iteration of my cursor loop.

Upvotes: 0

Views: 1508

Answers (3)

Priyanka Thakkar
Priyanka Thakkar

Reputation: 304

Get email from name::

 public String getEmail(String name, Context context){

    String email = null;
    String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like'%" +
            name + "%'";
    final String[] projection = new String[]{Email.DATA, // use
            // Email.ADDRESS
            // for API-Level
            // 11+
            Email.TYPE};
    Cursor c = context.getContentResolver().query
            (Email.CONTENT_URI,
                    projection, selection, null, null);
    if (c != null && c.moveToFirst()) {
        email = c.getString(0);
        c.close();
    }
    if (email == null)
        email = "Unsaved";

    Log.d(TAG, "email: " + email);
    return email;
}

Upvotes: 0

Ronak Mehta
Ronak Mehta

Reputation: 5979

Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.

 Cursor emailCur = cr.query( 
    ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
    null,
    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
    new String[]{id}, null); 
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)); 
} 
emailCur.close();

As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.

More tutorials here

Upvotes: 2

Gophermofur
Gophermofur

Reputation: 2101

I think you need to call emailCur.moveToFirst() before you call the while(...) loop.

Not sure if yours would work, but I always structure my cursor loops like this:

while(!emailCur.isAfterLast())
{
    //Do stuff with cursor

    emailCur.moveToNext();
}

Edit: Also, when you say you are looking up the email for a contact by it's display name, do you mean the contact's name (e.g. John Smith), or the display name of the email address? In the loop above, you are doing the latter.

Edit #2: Here is a tutorial on how to get the email address (and phone and address) for all contacts. You'll need to modify it slightly, so that the first portion returns only the contact for the display name you specify. The part about returning email addresses based on the _ID of the contact will still need to be done.

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

The modification to only get the ID of the contact that matches the display name you pass in will look very much like what you originally posted, with the exception of the of the URI you query and the data type you are matching the display name against (it will now become: ContactsContract.Contacts.DISPLAY_NAME).

Upvotes: 0

Related Questions