Getting error when try to get Uris for multiple contacts emails via contentResolver query

15 Views Asked by At

How to get photo Uris for multiple contacts emails

This code works fine for one email, but doesn't work for array of emails.

Error: Cannot bind argument at index 2 because the index is out of range. The statement has 1 parameters.

private fun getContactUri(email: Array<String>): Map<String, Uri?> {
        val contactsEmailsMapUri = mutableMapOf<String, Uri?>()
        val projection = arrayOf(ContactsContract.CommonDataKinds.Photo.CONTACT_ID)
        context.contentResolver.query(
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
            projection,
            ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?",
            email,
            null
        )?.use { uriCursor ->
            val columnIndex = uriCursor.getColumnIndex(
                ContactsContract.CommonDataKinds.Photo.CONTACT_ID
            )

            while (uriCursor.moveToNext()) {
                val contactIdString = uriCursor.getString(columnIndex)
                val contactIdLong = uriCursor.getLong(columnIndex)
                val uri = ContentUris.withAppendedId(
                    ContactsContract.Contacts.CONTENT_URI,
                    contactIdLong
                )
                val uriPath = Uri.withAppendedPath(
                    uri,
                    ContactsContract.Contacts.Photo.CONTENT_DIRECTORY
                )
                contactsEmailsMapUri.getOrPut(contactIdString) { uriPath }
            }
        }

        return contactsEmailsMapUri
    }
0

There are 0 best solutions below