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