2

According to the Android documentation it is possible to read the details from a specifically user-selected contact without the READ_CONTACTS permission. This should be possible by using the contacts URI, resulting from the "onActivityResult"-call. Querying this URI with a content resolver only gives me access to basic contact information though, as described in the ContactsContract.Contacts. I'm trying it using this (fairly standard) contact-picker code:

static final int REQUEST_SELECT_CONTACT = 1;

public void selectContact() {
    // Start an activity for the user to pick a contact
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_SELECT_CONTACT);
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
        // Get the URI and query the content provider for the contact data
        contactUri = data.getData();
        Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
        // If the cursor returned is valid, retrieve the contact's details
        if (cursor != null && cursor.moveToFirst()) {
            int numberIndex = cursor.getColumnIndex(Phone.NUMBER);
            int typeIndex = cursor.getColumnIndex(Phone.TYPE);
            int idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID);
            int nameIndex =cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
            String number = cursor.getString(numberIndex); // not accessible this way
            String type= cursor.getString(typeIndex); // not accessible this way
            String id = cursor.getString(idIndex); // accessible this way
            String name = cursor.getString(nameIndex); // accessible this way
        }
    }
}

As an example for all contact data that should be somehow accessible I'm using the phone number and type. The index of these results in -1 as they are not present in the cursor. Yet somehow I need to get them, but I'm stuck on how to do that. Any help is appreciated!

CM787
  • 73
  • 1
  • 5
  • 1
    I have deeply studied all documentation and questions but all lead to using the contact ID for a 2nd query. But this is prohibited without the READ_CONTACTS-permission. Another solution I tried without success is querying for the 'ContactsContract.Contacts.Entity', resulting in "java.lang.SecurityException: Permission Denial" – CM787 Jul 17 '15 at 10:31
  • 1
    Hi, have you found a solution to this situation? – Bianca Daniciuc Oct 06 '15 at 13:58
  • Sadly I haven't. The problem still persists and I'm stuck as I have no ideas on how to tackle it. Do you have the same issue? – CM787 Oct 07 '15 at 14:08
  • 1
    I do, but since I'm in a hurry, I've considered that adding the READ_CONTACTS permission wouldn't hurt that much, since few users, and even fewer developers care about one extra permission. I've almost lost the hope that it could be a solution to this, so for my case, using `ContactsContract.Contacts.Entity` (with permission) was the way to go. I'm still willing to hear about a clean way to do it, though. – Bianca Daniciuc Oct 07 '15 at 14:27
  • This post: http://stackoverflow.com/a/14082019/1606534 helped me understanding that all the contact details are spread across multiple tables (so that might be the reason to the whole permission mess) and explains how to get all the data you need. – Bianca Daniciuc Oct 07 '15 at 14:34

0 Answers0