Android 11. ContentResolver.query return empty cursor

Viewed 1623

I have implemented the selection of contacts from the phone book in my application. In order for the intent with action PICK to work on android 11, I added this to my manifest:

  <queries>
  <intent>
            <action android:name="android.intent.action.PICK" />
            <data android:mimeType="vnd.android.cursor.dir/phone_v2" />
        </intent>
</queries>

The code works fine on android versions 10 and below. But on android version 11, the contact I selected from the phone book is not inserted into the text field of my application, because ContentResolver.query return empty cursor. it.moveToFirst() returns false Here is my code:

  Constants.START_PICK_CONTACT_ACTION -> {
                data?.data?.let { uri ->
                    activity.contentResolver.query(uri, null, null, null, null)?.use {
                                if (it.moveToFirst()) {
                                    val number: String? = it.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                                        etPhoneNumber.setText(number)
                                    }
                                }
                            }
            }

Please, help me.

1 Answers

I've had a similar issue. Need to grant android.Manifest.permission.READ_CONTACTS permission before trying to call query

Related