0

I'm using a bit of code to get the ID of the current user's profile:

val cursor = contentResolver.query(
    ContactsContract.Profile.CONTENT_URI,
    arrayOf(ContactsContract.Profile._ID),
    null,
    null,
    null
)

cursor.moveToFirst()

val id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Profile._ID))
cursor.close()

On my Galaxy S7 device running Android 8.0.0 (API level 26) this works properly, but on the Nexus 5X emulator also running 8.0.0, I get CursorIndexOutOfBoundsException, implying that there is no user profile present.

My understanding was that since API 14 this method was guaranteed to return the user's profile, which exists by default. Why then is there no result?

Josh Hansen
  • 917
  • 1
  • 9
  • 20
  • 1
    Why did you think that? There's no requirement for it at all. A content provider will return an answer to a query. A fact like that would be implementation specific. Not all phones use the same contacts app. There is no assurance of having a self contact. – Gabe Sechan Nov 28 '18 at 20:46
  • 1
    Your have no guarantee that any data will be returned. The method `movetoFirst()` returns a boolean value indicating whether there is data or not. Only if it returns `true` should you continue. – Barns Nov 28 '18 at 20:48

0 Answers0