将邮件地址解析为联系人姓名

发布于 2024-09-06 17:23:08 字数 2011 浏览 2 评论 0原文

假设我的地址簿中保存了一个名为“TomTasche”的联系人,其邮件地址为“[电子邮件受保护]”。如果我只知道邮件地址,如何检索联系人的姓名?

我已经在此处提出了有关检索联系人昵称的类似问题,但是如果我更改该代码来查询邮件,就像这样:

Cursor cur = context.getContentResolver().query(Email.CONTENT_URI, new String[] {Email.DISPLAY_NAME, Email.TYPE, Email.CONTACT_ID}, Email.DATA + " = " + "[email protected]", null, null);

final int nameIndex = cur.getColumnIndex(Email.DISPLAY_NAME);
final int typeIndex = cur.getColumnIndex(Email.TYPE);
final int idIndex = cur.getColumnIndex(Email.CONTACT_ID);

if (cur.moveToFirst()) {
    name = cur.getString(nameIndex);
    type = cur.getString(typeIndex);
    id = cur.getString(idIndex);
} else {
    name = UNKNOWN;
}

它强制关闭,logcat 告诉我 @gmail 附近有语法错误。

06-22 09:21:14.260: ERROR/DatabaseUtils(110): Writing exception to parcel
06-22 09:21:14.260: ERROR/DatabaseUtils(110): android.database.sqlite.SQLiteException: near "@gmail": syntax error: , while compiling: SELECT display_name, data2, contact_id FROM view_data_restricted data WHERE (1 AND mimetype = 'vnd.android.cursor.item/phone_v2') AND (data1 = "Tom" <[email protected]>)

有趣的是,它似乎查询得很好。 Logcat 打印出联系人的姓名:

06-22 09:21:14.260: [...] AND (data1 = "Tom" <[email protected]>)

那么,我如何才能检索该姓名呢?

谢谢
Tom

编辑:

语法错误是由于缺少引号引起的,但光标仍然返回 null。

Assuming I have a contact saved in my addressbook, called "TomTasche" with the mail-address "[email protected]". How can I retrieve the contact's name, if I only know the mail-address?

I already asked a similar questions about retrieving contact's nickname here, but if I change that code to query the mail, like that:

Cursor cur = context.getContentResolver().query(Email.CONTENT_URI, new String[] {Email.DISPLAY_NAME, Email.TYPE, Email.CONTACT_ID}, Email.DATA + " = " + "[email protected]", null, null);

final int nameIndex = cur.getColumnIndex(Email.DISPLAY_NAME);
final int typeIndex = cur.getColumnIndex(Email.TYPE);
final int idIndex = cur.getColumnIndex(Email.CONTACT_ID);

if (cur.moveToFirst()) {
    name = cur.getString(nameIndex);
    type = cur.getString(typeIndex);
    id = cur.getString(idIndex);
} else {
    name = UNKNOWN;
}

it forces close and logcat tells me that there's a syntax error near @gmail.

06-22 09:21:14.260: ERROR/DatabaseUtils(110): Writing exception to parcel
06-22 09:21:14.260: ERROR/DatabaseUtils(110): android.database.sqlite.SQLiteException: near "@gmail": syntax error: , while compiling: SELECT display_name, data2, contact_id FROM view_data_restricted data WHERE (1 AND mimetype = 'vnd.android.cursor.item/phone_v2') AND (data1 = "Tom" <[email protected]>)

Interesting is that it seems to query fine. Logcat prints out the contact's name:

06-22 09:21:14.260: [...] AND (data1 = "Tom" <[email protected]>)

So, how am I able to retrieve the name?

Thanks

Tom

edit:

Syntax Error is caused by missing quotes, but cursor returns null anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油饼 2024-09-13 17:23:08

query()getColumnIndex() 中使用 Phone.DISPLAY_NAME 而不是 Email.DISPLAY_NAME(前 2 行) )。

编辑:

还有用于电子邮件查找的特殊 uri:Email.CONTENT_LOOKUP_URI
使用起来似乎更方便(不需要像代码中那样放置配额或手动编写选择参数):

Uri uri = Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode("[email protected]"));
Cursor cur = getContentResolver().query(uri, new String[]{Email.CONTACT_ID, mail.DISPLAY_NAME, Email.TYPE, Phone.DISPLAY_NAME}, null, null, null);

Use Phone.DISPLAY_NAME instead of Email.DISPLAY_NAME in both query() and getColumnIndex() (first 2 lines).

edit:

There's also special uri for e-mail lookups: Email.CONTENT_LOOKUP_URI.
It seems to be more convenient in use (no need to put quotas or manually compose selection argument as in your code):

Uri uri = Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode("[email protected]"));
Cursor cur = getContentResolver().query(uri, new String[]{Email.CONTACT_ID, mail.DISPLAY_NAME, Email.TYPE, Phone.DISPLAY_NAME}, null, null, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文