Search code examples
javaandroidcontactsandroid-contactsphone-number

Android, cant access contacts phone numbers


I cannot access the phone numbers of my contacts and can't figure out why.

private void getContacts(){
        Cursor people = mCon.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (people.moveToFirst()) {
            do {
               int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
               String contact = people.getString(nameFieldColumnIndex).toLowerCase();
               int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
               String number = people.getString(numberFieldColumnIndex); 
               hm.put(contact, number);
            }while(people.moveToNext());
        }
        people.close();
    }

The names are going into the hashmap fine but numberFieldColumnIndex keeps coming out as -1

I output the column headings of people and there is not one for number? How can I retrieve them?

List column headings of People

02-18 13:26:38.500: D/Contacts(1587): 0 times_contacted
02-18 13:26:38.500: D/Contacts(1587): 1 contact_status
02-18 13:26:38.500: D/Contacts(1587): 2 phonetic_name
02-18 13:26:38.500: D/Contacts(1587): 3 phonetic_name_style
02-18 13:26:38.500: D/Contacts(1587): 4 link
02-18 13:26:38.500: D/Contacts(1587): 5 is_user_profile
02-18 13:26:38.500: D/Contacts(1587): 6 raw_contact_linkpriority4
02-18 13:26:38.500: D/Contacts(1587): 7 raw_contact_linkpriority5
02-18 13:26:38.500: D/Contacts(1587): 8 lookup
02-18 13:26:38.500: D/Contacts(1587): 9 contact_status_icon
02-18 13:26:38.500: D/Contacts(1587): 10 last_time_contacted
02-18 13:26:38.500: D/Contacts(1587): 11 sec_custom_vibration
02-18 13:26:38.500: D/Contacts(1587): 12 _id
02-18 13:26:38.500: D/Contacts(1587): 13 display_name_source
02-18 13:26:38.500: D/Contacts(1587): 14 photo_uri
02-18 13:26:38.500: D/Contacts(1587): 15 photo_thumb_uri
02-18 13:26:38.500: D/Contacts(1587): 16 contact_chat_capability
02-18 13:26:38.500: D/Contacts(1587): 17 photo_id
02-18 13:26:38.500: D/Contacts(1587): 18 send_to_voicemail
02-18 13:26:38.500: D/Contacts(1587): 19 display_name_reverse
02-18 13:26:38.500: D/Contacts(1587): 20 custom_ringtone
02-18 13:26:38.500: D/Contacts(1587): 21 name_raw_contact_id
02-18 13:26:38.500: D/Contacts(1587): 22 photo_file_id
02-18 13:26:38.500: D/Contacts(1587): 23 has_phone_number
02-18 13:26:38.500: D/Contacts(1587): 24 link_type5
02-18 13:26:38.500: D/Contacts(1587): 25 link_type4
02-18 13:26:38.500: D/Contacts(1587): 26 link_type3
02-18 13:26:38.500: D/Contacts(1587): 27 contact_status_label
02-18 13:26:38.500: D/Contacts(1587): 28 link_type2
02-18 13:26:38.500: D/Contacts(1587): 29 link_type1
02-18 13:26:38.500: D/Contacts(1587): 30 raw_contact_linkpriority3
02-18 13:26:38.500: D/Contacts(1587): 31 raw_contact_linkpriority2
02-18 13:26:38.500: D/Contacts(1587): 32 display_name
02-18 13:26:38.500: D/Contacts(1587): 33 raw_contact_linkpriority1
02-18 13:26:38.500: D/Contacts(1587): 34 has_email
02-18 13:26:38.500: D/Contacts(1587): 35 sort_key_alt
02-18 13:26:38.500: D/Contacts(1587): 36 dirty_contact
02-18 13:26:38.500: D/Contacts(1587): 37 in_visible_group
02-18 13:26:38.500: D/Contacts(1587): 38 starred
02-18 13:26:38.500: D/Contacts(1587): 39 link_count
02-18 13:26:38.500: D/Contacts(1587): 40 display_name_alt
02-18 13:26:38.500: D/Contacts(1587): 41 sort_key
02-18 13:26:38.500: D/Contacts(1587): 42 contact_presence
02-18 13:26:38.500: D/Contacts(1587): 43 contact_status_res_package
02-18 13:26:38.500: D/Contacts(1587): 44 contact_status_ts

Solution

  • Following code shows an easy way to read all phone numbers and names:

    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while (phones.moveToNext())
    {
      String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
    }
    phones.close();