将邮件地址解析为联系人姓名
假设我的地址簿中保存了一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
query()
和getColumnIndex()
中使用Phone.DISPLAY_NAME
而不是Email.DISPLAY_NAME
(前 2 行) )。编辑:
还有用于电子邮件查找的特殊 uri:
Email.CONTENT_LOOKUP_URI
。使用起来似乎更方便(不需要像代码中那样放置配额或手动编写选择参数):
Use
Phone.DISPLAY_NAME
instead ofEmail.DISPLAY_NAME
in bothquery()
andgetColumnIndex()
(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):