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.
<queries>
for that code, though you might from some other code. By "empty cursor", do you mean it is a validCursor
but has no rows (soit.moveToFirst()
returnsfalse
)? – Calichequery()
call, try specifying the projection that you need (2nd parameter) instead of passingnull
, and see if that helps. – Calicheandroid.Manifest.permission.READ_CONTACTS
permission in manifest? – Flesher