Android 11. ContentResolver.query return empty cursor
Asked Answered
H

2

5

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.

Horseshit answered 29/9, 2020 at 12:45 Comment(6)
You do not need <queries> for that code, though you might from some other code. By "empty cursor", do you mean it is a valid Cursor but has no rows (so it.moveToFirst() returns false)?Caliche
@CommonsWare, Without <queries> queryIntentActivities return null. Yes, it.moveToFirst() returns falseHorseshit
"Without <queries> queryIntentActivities return null" -- agreed, but that code is not in your question. In your query() call, try specifying the projection that you need (2nd parameter) instead of passing null, and see if that helps.Caliche
@CommonsWare,The cursor is still empty(Horseshit
@Horseshit Were you able to resolve this problem?Photogene
Have you added android.Manifest.permission.READ_CONTACTS permission in manifest?Flesher
L
6

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

Larghetto answered 7/10, 2020 at 12:53 Comment(0)
N
1

I encountered the same issue. After trying many times, I found that:

a) Please double check if you have declared READ_CONTACTS permission in AndroidManifest.xml. If yes, we need to gain the runtime permission for READ_CONTACTS. Otherwise the ContentResolver.query() just return an empty cursor.

AndroidManifest.xml looks like:

<manifest>
...
  <uses-permission android:name="android.permission.READ_CONTACTS" />
...
  <queries>
    <intent>
      <action android:name="android.intent.action.PICK" />
      <data android:mimeType="vnd.android.cursor.dir/phone_v2" />
    </intent>
  </queries>
</manifest>

Code snippet:

activity?.let {
    if (ContextCompat.checkSelfPermission(it, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
        //openContact()
    } else {
        requestPermissions(
            arrayOf(Manifest.permission.READ_CONTACTS),
            REQUEST_PERMISSION_READ_CONTACT_CODE
        )
    }
}

...

override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
  // openContact()
}

b) If there is no declaration of READ_CONTACTS permission in manifest, just add query element is enough.

AndroidManifest.xml looks like:

<manifest>
...
  <uses-permission android:name="android.permission.READ_CONTACTS" />
...
  <queries>
    <intent>
      <action android:name="android.intent.action.PICK" />
      <data android:mimeType="vnd.android.cursor.dir/phone_v2" />
    </intent>
  </queries>
</manifest>

But I cannot find any documentation can explain this behavior, does anyone know the root cause? Would appreciate it very much if you could tell.

Nomanomad answered 7/11, 2022 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.