0

I'm using ACTION_PICK intent for contacts and I want to extract the name and phone from the picked contact.

private static final int CONTANTS_REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_urgent_contacts);

    Intent contact_picker = new Intent(Intent.ACTION_PICK);
    contact_picker.setType(ContactsContract.Contacts.CONTENT_TYPE);
    startActivityForResult(contact_picker, CONTANTS_REQUEST_CODE);


}

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

    if(requestCode == CONTANTS_REQUEST_CODE && resultCode == RESULT_OK){
        onContactPicked(data);
    }
}

private void onContactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String contact_phoneNo =null;
        String contact_name;
        Uri uri = data.getData();
        cursor = getContentResolver().query(uri, null, null, null, null);
        if(cursor != null && cursor.moveToFirst()){
            int  nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            contact_name = cursor.getString(nameIndex);

            Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            String phone = null;
            if (hasPhone > 0) {
                int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                contact_phoneNo = cursor.getString(phoneIndex);


            }

            Toast.makeText(this, contact_name+" was added to your urgent contact list", Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "phone number: "+contact_phoneNo, Toast.LENGTH_SHORT).show();
        }

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

Rereiving contact name works just fine. The problem is with phone number. The app is not crashing but I see an error in Logcat pointing to contact_phoneNo = cursor.getString(phoneIndex)

Thanks for the help

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Yael Pesso
  • 53
  • 8
  • Possible duplicate: https://stackoverflow.com/questions/11218845/how-to-get-contacts-phone-number-in-android – you query the `Contacts` table, but the column `ContactsContract.CommonDataKinds.Phone.NUMBER` is in the `Data` table. – dst Sep 16 '18 at 15:32
  • When I change it to ContactsContract.CommonDataKinds.Phone.NUMBER it shows me the same name and phone, no matter what I pick – Yael Pesso Sep 16 '18 at 16:07

2 Answers2

0

The picked contact may have no phones, or may have multiple phones, in which case the app may get a different phone then the user intended to pick.

Instead of the contact-picker you should use the phone-picker, which allows to user to select a contact's specific phone, like so:

static final int REQUEST_SELECT_PHONE_NUMBER = 1;

public void selectContactPhone() {
    // Start an activity for the user to pick a phone number from contacts
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(CommonDataKinds.Phone.CONTENT_TYPE); // switches to PHONE PICKER
    startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the phone number
        Uri contactUri = data.getData();
        String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
        Cursor cursor = getContentResolver().query(contactUri, projection,
                null, null, null);
        // If the cursor returned is valid, get the phone number
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
            String number = cursor.getString(numberIndex);
            // Do something with the phone number
            ...
        }
    }
}

For more info, see: https://developer.android.com/guide/components/intents-common.html#Contacts

marmor
  • 27,641
  • 11
  • 107
  • 150
0

what you can do for your requirement is,

private void getContactsList() {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(String.valueOf(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE));
        startActivityForResult(intent, CONTANTS_REQUEST_CODE);
    }

now, to get the details of the selected contact,

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CONTANTS_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};

            Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
            cursor.moveToFirst();

            int nameColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            name = cursor.getString(nameColumnIndex);

            int numberColumnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            number = cursor.getString(numberColumnIndex);

            if (TextUtils.isEmpty(number)) {
                Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
                while (phones.moveToNext()) {
                    number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                phones.close();
            }              

            Log.d("dataContact", " Name : " + name + ", Number : " + number);                                    
        }
    }
}

this is the best solution for your case.

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49