zuc0001
zuc0001

Reputation: 930

Fetch email from the selected Contact in Android APK 25

Code to get permissions and start Pick Contact Activity

Button chooseContactsBtn = (Button) findViewById(R.id.addContactButton);
    chooseContactsBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getPermissionToReadUserContacts();
            Intent contacts = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
            startActivityForResult(contacts, PICK_CONTACTS);
        }
    });

On Activity Result

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACTS) {
        if (resultCode == RESULT_OK) {
            ContactDataManager contactsManager = new ContactDataManager(this, data);

            try {

                email = contactsManager.getContactEmail();
                EditText e_mail = (EditText) findViewById(R.id.e_mail);
                e_mail.setText(email);

            } catch (ContactDataManager.ContactQueryException e) {
                //Print Exception
            }
        }
    }
}

getContactEmail Method in Contact Manager class.

public String getContactEmail() throws ContactQueryException
{
    Cursor cursor = null;
    String email = null;
    try
    {
        cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?",
                new String[]{intent.getData().getLastPathSegment()},
                null);

        if (cursor.moveToFirst())
        {
            email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
        }
        else {
            System.out.println("No Email found. and leaving the method.");
        }


    } catch (Exception e)
    {
        Log.e(LOG_TAG, e.getMessage());
        throw new ContactQueryException(e.getMessage());
    } finally
    {
        if (cursor != null)
            cursor.close();
    }

    return email;
}

This doesn't return anything. I have methods for returning name and number and they work fine. Contact permission is positive.

Upvotes: 0

Views: 47

Answers (1)

marmor
marmor

Reputation: 28239

You're invoking the phone picker intent (via the CommonDataKinds.Phone.CONTENT_URI uri in your intent).

You can do one of two things: (a) change your picker intent to be an email picker, and get a specific email in return (b) change your picker intent to be a contact picker, and get the first email found for that contact (if any), by using your getContactEmail which is based on a CONTACT_ID.

I'd go for option (a), code is:

Intent contacts = new Intent(Intent.ACTION_PICK, CommonDataKinds.Email.CONTENT_URI); // Note the Email!
startActivityForResult(contacts, PICK_CONTACTS);

and then:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACTS && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the EMAIL
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Email.ADDRESS};
        Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
        // If the cursor returned is valid, get the email
        if (cursor != null && cursor.moveToFirst()) {
            String email = cursor.getString(0);
        }
    }
}

See more at: https://developer.android.com/guide/components/intents-common.html#Contacts

Upvotes: 1

Related Questions