0

I have used an intent of contact picker. I am getting the data of contact through the intent. By that that intent I am fetching the name, number of the contact person.

When I tried to fetch an email id of contact it only shows the number of the contact instead of an email id. Also for last name of contact it shows always null though the last is been set to the contact.

Code:

    private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null ;
        String name = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

        int contactIdIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);

        int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

        int lastNameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);


        mOrganizerPhone = cursor.getString(phoneIndex);
        mOraganizerName = cursor.getString(nameIndex);
        mOrganizersId = cursor.getString(contactIdIndex);
        mOrganizersEmail = cursor.getString(emailIndex);
        mOrganizersLastName = cursor.getString(lastNameIndex);


    Toast.makeText(PlanEventActivity.this,name+" "+phoneNo,Toast.LENGTH_SHORT).show();


    } catch (Exception e) {
        e.printStackTrace();
    }
}

Thank you..

Sid
  • 2,792
  • 9
  • 55
  • 111
  • 1
    I think this helpful for you: http://stackoverflow.com/questions/12109391/getting-name-and-email-from-contact-list-is-very-slow – xxx Jun 23 '16 at 11:06
  • could you please let me know the query to be used? Because as I have searched i see the different query's for getting email and structured last name. @HuyN – Sid Jun 23 '16 at 11:59

1 Answers1

0

With "id", you can get the email address like this, this is an example:

public class CustomerForm extends Activity {
private final static int CONTACT_PICKER = 1;
private EditText txtMailContacto;
private EditText txtNombreContacto;
private EditText txtTelefono;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_customer_form);
    txtMailContacto = (EditText) findViewById(R.id.txtMailContacto);
    txtTelefono = (EditText) findViewById(R.id.txtTelefono);
    txtNombreContacto = (EditText) findViewById(R.id.txtNombreContacto);
}
public void pickContact(View v)
{
    Intent contactPickerIntent = 
            new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // check whether the result is ok
    if (resultCode == RESULT_OK) {
        // Check for the request code, we might be using multiple startActivityForReslut
        switch (requestCode) {
        case CONTACT_PICKER:
            contactPicked(data);
            break;
        }
    } else {
        Log.e("MainActivity", "Failed to pick contact");
    }
}
private void contactPicked(Intent data) {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    cur.moveToFirst();
    try {
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        cur = getContentResolver().query(uri, null, null, null, null);
        cur.moveToFirst();
                                                // column index of the contact ID
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                                                // column index of the contact name 
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        txtNombreContacto.setText(name);        //print data            
                                                // column index of the phone number
        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                    new String[]{id}, null);
                    while (pCur.moveToNext()) {
                        String phone = pCur.getString(
                                pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        txtTelefono.setText(phone);         //print data
                    } 
                    pCur.close();
                                                // column index of the email   
        Cursor emailCur = cr.query(        
                ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                null,
                ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  
                new String[]{id}, null); 
            while (emailCur.moveToNext()) { 
                // This would allow you get several email addresses
                    // if the email addresses were stored in an array  
                String email = emailCur.getString(
                              emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));                                         
                txtMailContacto.setText(email);         //print data
        } 
        emailCur.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}
xxx
  • 3,315
  • 5
  • 21
  • 40
  • for each id i need to create cursor for email? and what about last name? @Huy N – Sid Jun 23 '16 at 12:18
  • That's right! About the last name, my solution above use "ContactsContract.Contacts.DISPLAY_NAME" and it get the full name, You can use spit method in Java to get the name you want :) – xxx Jun 23 '16 at 12:22
  • I tried this solution not getting an email id. @Huy N – Sid Jun 23 '16 at 12:34
  • it dose not go into the while loop of email cursor. @Huy N – Sid Jun 23 '16 at 12:35
  • yes I have the permissions. I can see the id and display name but not an emailid. @Huy N – Sid Jun 23 '16 at 12:38
  • @Sid You want to get email id for what? There's no emailID. With contact id (ContactsContract.Contacts._ID) you can get the email :| – xxx Jun 23 '16 at 12:43
  • sorry dint get you? @Huy N – Sid Jun 23 '16 at 12:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115408/discussion-between-sid-and-huy-n). – Sid Jun 23 '16 at 12:49
  • get email by "contact Id", there's no such thing called "email Id" String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); – xxx Jun 23 '16 at 12:53