1

I am trying to use provider ContactsContract for retrieving contacts information. Before fetching the Phone Numbers of contacts, I am checking whether contacts have phone numbers. For that I am using uri ContactsContract.Contacts.HAS_PHONE_NUMBER but on some devices it returns 1 or 0 and returns NULL on others. I am testing on Android 2.3.1, it returns NULL here. When tried to debug, String phonenumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));, It shows the mobile number. Can anyone please point out why the behaviour is different. I have checked the API of ContactsContract, its level 5 which means devices 2.3+ can use this. I am getting the following exception:

Exception

06-03 13:01:51.065: E/AndroidRuntime(545): java.lang.RuntimeException: 
Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.android.contacts/data/2 flg=0x1 (has extras) }} 
to activity {com.tzoomers.birthdaysdiary/com.tzoomers.birthdaysdiary.BirthdaysDiary}:
java.lang.IllegalStateException: get field slot from row 0 col -1 failed

Start Contacts Picker Activity

private void addFromContacts()
    {
        Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds
                .Phone.CONTENT_URI);
        this.context.startActivityForResult(contactIntent, CONTACT_SELECTED);
    }

onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode)
        {
        case CONTACT_SELECTED:
            if(resultCode == Activity.RESULT_OK)
            {
                Uri contactData = data.getData();
                Cursor c =  managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    int cId;
                    String phoneNumber = "";
                  String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                  cId = c.getInt(c.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID));

                  int hasPhone = Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));

                  if(hasPhone > 0)
                  {
                    phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  }
                }
            }
            break;
        }
    }
Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46

3 Answers3

0

Try using **ContactsContract.Contacts.CONTENT_URI** instead of ContactsContract.CommonDataKinds.Phone.CONTENT_URI in your Start Contacts Picker Activity

VikramV
  • 1,111
  • 2
  • 13
  • 31
0

add the below permisions to your manifest file:

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0

if you are using Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); you will just see those contacts which have a number, so you dont need to check with ContactsContract.Contacts.HAS_PHONE_NUMBER. But if you are using Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); you will see all contacts (also those without a number) and then you can check with ContactsContract.Contacts.HAS_PHONE_NUMBER and it should work fine

user3466562
  • 465
  • 2
  • 10
  • 23