0

I'm trying to get the Email address of a user I select in the contact list. I am able to get the ID, phone and name of the contact and by using the ID, I am trying to get the Email address using a new Cursor but am unable to get the details from the contact.

Below is the code I am using:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
    super.onActivityResult(requestCode, resultCode, aData);
    if (requestCode == PICK_CONTACT && resultCode == Activity.RESULT_OK && aData != null) {

        ContentResolver cr = getContentResolver();
        String phoneNo = "";
        String name = "";
        String id = "";
        String email = "";
        Uri uri = aData.getData();
        if (uri != null) {
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    phoneNo = cursor.getString(phoneIndex);
                    name = cursor.getString(nameIndex);
                }
                cursor.close();
            } else {
                showDialog(getString(R.string.lbl_error_message_contact));
            }
        } else {
            showDialog(getString(R.string.lbl_error_message_contact));
        }
        Cursor emailCur = cr.query(
                ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                new String[]{id}, null);
        while (emailCur.moveToNext()) { // when it gets here, it just skips the while loop and jumps down to the to close the emailCur
            // This would allow you get several email addresses
            // if the email addresses were stored in an array
            email = emailCur.getString(
                    emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
        }
        emailCur.close();

        mMedia.setLendersName(name);
        mMedia.setPhoneNumber(phoneNo);
        mMedia.setEmail(email);


    }
 }

Could it be that the ID may not be correct?

EDIT

One thing that is really weird is that when I select a contact and return back to the screen. The ID is very different to the ID I get when I run @Android Team's code which returns all the contacts with email addresses. What could be the reason behind this?

My thinking when I was trying to get the email address was that the ID's wont be different, but it seems it is the case.

Stillie
  • 2,647
  • 6
  • 28
  • 50
  • 1
    Have a look at [This discussion](https://stackoverflow.com/questions/10117049/get-only-email-address-from-contact-list-android). – ADM Mar 06 '18 at 09:03
  • thanks, To me it looks like that method in the discussion gets all the contacts that have email address, what i am doing is opening the contacts list using an intent, selecting a contact, then the details of that contact are returned, hence the onActivityResult method. It will be a heavy task to get all the contacts on the device, just to get one email account – Stillie Mar 06 '18 at 09:08
  • Yeah i know but the Provider is same . Just query `ContactsContract.Contacts.CONTENT_URI`. – ADM Mar 06 '18 at 09:11

3 Answers3

0

when you getting device contact information you can add permission in manifest file..

<uses-permission android:name="android.permission.READ_CONTACTS"/>

then after used below method to get all the email id which device store in contact..

    /**
 * this method read device in contact name and email.
 */
public void getContact() {
        Cursor cur = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                Cursor cur1 = getActivity().getContentResolver().query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (cur1.moveToNext()) {
                    //to get the contact names
                    String name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    Log.e("Name :", name);
                    String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    Log.e("Email", email);
                    }
                cur1.close();
            }
        }

}

check in log cat show all email id to be added in device.

0

I hope you know the Permission part and all, just add this code once you are allowed permission by user, and populate the email string in Edittext use it wherever you want

Account[] accounts = AccountManager.get(activityContext).getAccountsByType("com.google");
                    for (Account account : accounts) {
                        // if (emailPattern.matcher(account.name).matches()) {
                        String possibleEmail = account.name;
                    }

You might have to use this piece of code in activity to Populate the data, just an Add On

runOnUiThread(new Runnable() {
        public void run() {
            regEmail.setText(emailId);
        }
    });
Akshay
  • 318
  • 1
  • 15
  • permissions are not a problem, i am already able to get the name and telephone number from the contact account, i cannot get the email – Stillie Mar 06 '18 at 09:24
  • did you try the above Manifest.permission.READ_CONTACTS – Akshay Mar 06 '18 at 09:25
0

I don't think some of you understood what I was trying to say, but this is the solution I found to get the Contact details of a selected contact by starting an intent to get the details from the Systems contact list.

Start the intent with the following intent to get the Name and Phone number:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);

Once the contact is selected, it comes back into onActivityResult. Here is an example of getting the Name and Phone number from the response.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
    super.onActivityResult(requestCode, resultCode, aData);

    if (requestCode == PICK_CONTACT && resultCode == Activity.RESULT_OK && aData != null) {
        final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA, ContactsContract.CommonDataKinds.Email.TYPE};
        ContentResolver cr = getContentResolver();
        String phoneNo;
        String name;
        String lookUpKey = "";
        String email;
        Uri uri = aData.getData();
        if (uri != null) {
            // ****************************First we want to get the Lookup Key, Name and Phone number****************************
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    lookUpKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    phoneNo = cursor.getString(phoneIndex);
                    name = cursor.getString(nameIndex);
                    mMedia.setLendersName(name);
                    mMedia.setPhoneNumber(phoneNo);
                }
                // ****************************Now we want to get the email address, if any from the contact****************************
                // At this point, we use the Lookup key of the contact above, to get the email address (If any)
                if (!TextUtils.isEmpty(lookUpKey)) {
                    //Do a query with the Lookup key to get the email details (Could possibly get more data as well, but I just needed the email address
                    cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.LOOKUP_KEY + "=?", new String[]{lookUpKey}, null);
                    if (cursor != null) {
                        while (cursor.moveToNext()) {
                            // Get the index of the column for the email address here
                            final int contactEmailColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

                            while (!cursor.isAfterLast()) {
                                // Here is where we get the email address itself
                                email = cursor.getString(contactEmailColumnIndex);
                                // Set it where ever you need it.
                                mMedia.setEmail(email);
                                cursor.moveToNext();
                            }
                        }
                        // Dont forget to close the cursor once you done with it
                        cursor.close();
                    }
                }
            } else {
                showDialog(getString(R.string.lbl_error_message_contact));
            }
        } else {
            showDialog(getString(R.string.lbl_error_message_contact));
        }
    }
}
Stillie
  • 2,647
  • 6
  • 28
  • 50