我正在尝试从联系人处获取电子邮件地址。我阅读了Stackoverflow上的所有帖子,但没有找到解决方案。这是我正在使用的代码
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK){
if (requestCode == Constants.REQUEST_CODE_PICK_CONTACT){
Uri contactUri = data.getData();
String email = getEmail(contactUri);
...
这是获取电子邮件的函数:
public String getEmail(Uri uriContact){
String contactID = uriContact.getLastPathSegment();
String email = null;
Cursor cursorData = getActivity().getContentResolver().query(Email.CONTENT_URI,
null,
Email.CONTACT_ID + " = " + contactID,
null,
null);
if (cursorData != null){
if (cursorData.moveToFirst()){
email = cursorData.getString(cursorData.getColumnIndex(Email.DATA));
}
}
cursorData.close();
return email;
}
我也尝试使用以下代码获取contactID:
// getting cursor for ID
Cursor cursorID = getActivity().getContentResolver().query(uriContact,
null,
null, null, null);
// getting selected contact ID
if (cursorID != null){
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
}
但结果仍然相同。此外,我已成功从联系人获得电话和显示名称,但电子邮件代码不起作用。电子邮件光标始终为空。我做错了什么?