0

I have seen many threads for accessing contacts from address book, but apart from name and number, i want to access other details like- city, state, country, postal code of the person. I have tried this code, but other things are null.

                Cursor c = null;
                try 
                {
                     Uri uri = data.getData();
                    c = getContentResolver().query(uri, new String[]
                            { 
                                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                                ContactsContract.CommonDataKinds.Phone.NUMBER,  
                                ContactsContract.CommonDataKinds.StructuredPostal.CITY,
                                ContactsContract.CommonDataKinds.StructuredPostal.STREET,
                                ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY },
                            null, null, null);

                    if (c != null && c.moveToFirst()) 
                    {
                        name = c.getString(0);
                        String number = c.getString(1);
                        city = c.getString(2);
                        street = c.getString(3);
                        country = c.getString(4);
                     Log.e("contactData...", name + ", " + street + ", " + number + ", " + city + ", " + country);
                    }
                } 
                finally 
                {
                    if (c != null) 
                    {
                        c.close();
                    }
                }
amit singh
  • 1,407
  • 2
  • 16
  • 25
  • Please refer here:- http://stackoverflow.com/questions/18181204/when-i-select-contact-name-and-address-from-contact-list-address-data-not-retrie – Sachchidanand Mar 09 '16 at 11:55

1 Answers1

0

If the contact does not have any value for the address, then you cannot get anything but null. Looking over it quickly, your code seems fine. Test it out with a contact that has their address set. You can't expect the phone to just randomly come up with the user's address, can you?

One issue with using this in an app would be that many users do not fill out anything besides name, phone number, or email. It would be best to check if the address is null before trying to use the value.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • Thanks for answering, i tried above code for that contact which has full address details, but its giving me only name and number. – amit singh Mar 02 '14 at 16:21