0

I'm trying to get email address from contacts. I read all the posts on Stackoverflow but did not found a solution. Here is code that i'm using

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);
                ...

Here is func to get email :

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;
}

I'm also have tried to get contactID by using following code :

    // 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));
        }
    }

But result still the same. Also I'm successfully got Phone and Display name from contact, but email code dont work. Email cursor is always empty. what am I doing wrong ?

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
Alex Perevozchykov
  • 2,211
  • 22
  • 19
  • did u get any error while excuting application?if yes then post your error message. – Amarnath Baitha Mar 11 '14 at 09:02
  • no, there is no any error, but your code works fine and i have noticed that in your code ID of the contact is different that in my code and probably this is the reason that ia cant get email. But i dont understand why – Alex Perevozchykov Mar 11 '14 at 09:26

0 Answers0