0

Im having some trouble with getting a phone number from a contact using the ContactsContract My code is

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
if (resultCode == RESULT_OK) {  
    switch (requestCode) {  
    case CONTACT_PICKER_RESULT:  
           Cursor cursor = null;
           String phone = "";
           try {
               Bundle extras = data.getExtras();
               Set<String> keys = extras.keySet();
               Iterator<String> iterate = keys.iterator();
               while (iterate.hasNext()) {
                   String key = iterate.next();
                   Log.v(DEBUG_TAG, key + "[" + extras.get(key) + "]");
               }

               Uri result = data.getData();
               Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());

               // get the contact id from the Uri
               String id = result.getLastPathSegment();
               cursor = getContentResolver().query(Phone.CONTENT_URI,null, Phone.CONTACT_ID + "=?", new String[] { id },null);

               int PhoneIdx = cursor.getColumnIndex(Phone.NUMBER);


               if (cursor.moveToFirst()) {
                   phone = cursor.getString(PhoneIdx);
                   Log.v(DEBUG_TAG, "Got number: " + phone);

               } else {
                   Log.w(DEBUG_TAG, "No results");
               }

           } catch (Exception e) {
               Log.e(DEBUG_TAG, "Failed to get Number", e);

           } finally {
               if (cursor != null) {
                   cursor.close();
               }
               EditText ponenumber = (EditText) findViewById(R.id.editPhone1);
               ponenumber.setText(phone);

               if (phone.length() == 0) {
                   Toast.makeText(this, "Contact has no phone number",
                           Toast.LENGTH_LONG).show();
               }

           }
        break;  
    }  

} else {  
    Log.w(DEBUG_TAG, "Warning: activity result not ok");  
}  

}

Whenever i run it and choose a contact i just get the "Contact has no phone number". I can't figure out why, anyone has any ideas?

Regards

Fredkr
  • 723
  • 3
  • 8
  • 21

1 Answers1

0

You should use a "Phone picker" intent, not a "Contact picker":

static final int REQUEST_SELECT_PHONE_NUMBER = 1;

public void selectContact() {
    // 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);
    if (intent.resolveActivity(getPackageManager()) != null) {
        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
            ...
        }
    }
}

Taken from the docs at: https://developer.android.com/guide/components/intents-common.html#Contacts

To have the user select a specific piece of information from a contact, such as a phone number, email address, or other data type, use the ACTION_PICK action and specify the MIME type to one of the content types listed below, such as CommonDataKinds.Phone.CONTENT_TYPE to get the contact's phone number.

marmor
  • 27,641
  • 11
  • 107
  • 150