2

I need to get the address of some contact from my contacts list. I use the ACTION_PICK intent in order to do that:

            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE);                
            startActivityForResult(intent, ActivitiesRequestCodes.REQUEST_CODE_PICK_CONTACT);

When receiving the result in onActivityResult, I parse the result in order to obtain the contact's address:

           Cursor cursor = getContentResolver().query(contactDataUri, null,null, null, null);

           StringBuilder address = new StringBuilder();

           if (cursor != null && cursor.moveToFirst()) {
                 // read street
                int streetIndex =     cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET);
                String street = cursor.getString(streetIndex);
                 // read city etc .....

          }

This code was written based on : The result Intent delivered to your onActivityResult() callback contains the content: URI pointing to the selected contact data. The response grants your app temporary permissions to read that contact data even if your app does not include the READ_CONTACTS permission. (http://developer.android.com/guide/components/intents-common.html#Contacts)

Everything works perfect on Samsung devices, but suprise! on Htc devices the parsing code crashes with Security exception:

Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.HtcContactsProvider2 uri content://com.android.contacts/data/1478 from pid=11661, uid=10197 requires android.permission.READ_CONTACTS, or grantUriPermission()

What to do next? I must not use READ_CONTACTS permission. Any ideas would be appreciated.

Thanks

AVirvara
  • 565
  • 5
  • 7
  • 1
    You can not do it without permission. Better is "do not do anything". What is issue while using permission? – Pankaj Kumar Oct 08 '14 at 11:36
  • 1
    Permission is mandatory.. HTC android version?? – Utpal Sharma Oct 08 '14 at 11:42
  • `I must not use READ_CONTACTS permission` This is NONSENSE: **you must** use that permission. – Phantômaxx Oct 08 '14 at 11:45
  • "This code was written based on..." -- apparently, validating this is not part of the Compatibility Test Suite. Personally, I am *far* more comfortable with HTC's approach; I will be filing complaints with the Android team regarding the documented behavior. Also, I am not aware that this behavior has always been supported, so your code without the permission may fail on older devices. IMHO, either request the permission, live without the data in general, or be able to gracefully degrade when the device will not give you the data without the permission. – CommonsWare Oct 08 '14 at 11:48
  • Many users complained about it, they got suspicious about us reading their contacts. in fact, we just display the contact's address on the map. – AVirvara Oct 08 '14 at 11:51

1 Answers1

0

From Contact picker you will get the contact details without adding the permission try this code

 Cursor cursor = null;
    try {
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();
        //Query the content uri
        String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
        cursor = context.getContentResolver().query(uri, projection, null, null, null);
        cursor.moveToFirst();
    } catch (Exception e) {
    }
Jithu P.S
  • 1,843
  • 1
  • 19
  • 32