user3217900
user3217900

Reputation: 35

Get Contact name from adress book given email id

If I have the email id and I need to get the corresponding name from the address book, is it possible to do so?

Upvotes: 1

Views: 93

Answers (2)

Umang Kothari
Umang Kothari

Reputation: 3694

may this work for you :

    public String getCallerFromEmailId(String emailId) {
    // TODO Auto-generated method stub
    String callerName = null;
    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {

            Cursor pcursor = resolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Email.DATA
                            + " = ?", new String[] { emailId }, null);

            while (pcursor.moveToNext()) {
                callerName = pcursor
                        .getString(pcursor
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                return callerName;
            }
            pcursor.close();

        }
    }
    cursor.close();
    return callerName;
}

Upvotes: 0

Jitendra
Jitendra

Reputation: 319

Fetch contacts based on emailID using email match specific URI

Uri uri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_FILTER_URI, Uri.encode(emailIdString));

where emailIdString is the email address for which you want to fetch contacts.

Cursor contactLookup = cr.query(uri, new String[] {ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.Data.DISPLAY_NAME }, null, null, null);

contactLookup cursor will have all contacts whose emailID matches with the emailIdString.

Upvotes: 1

Related Questions