Getting Contact Email By Name
Solution 1:
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.
CursoremailCur= cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
newString[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses// if the email addresses were stored in an arrayStringemail= emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
StringemailType= 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
Solution 2:
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
).
Solution 3:
Get email from name::
publicStringgetEmail(String name, Context context){
String email = null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " like'%" +
name + "%'";
final String[] projection = newString[]{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;
}
Post a Comment for "Getting Contact Email By Name"