I am trying to get an address from a phones contact with no luck, I am able to get the name, phone number and email, but not the address. When debugging the cursor for StructuredPostal looks the same as for the phone number. I have 'android.permission.READ_CONTACTS' set in my manifest, hence getting the other info. This is the code I'm using:
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickContact(),
onResult = {
val contentResolver: ContentResolver = ctx.contentResolver
val cursor: Cursor? = contentResolver.query(
it!!,
null,
null,
null,
null)
if (cursor != null) {
if (cursor.moveToFirst()) {
changed = true
clientUiState.clientDetails.clientname =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
Log.d("Name", clientUiState.clientDetails.clientname)
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val postal: Cursor? = contentResolver.query(
StructuredPostal.CONTENT_URI,
null,
StructuredPostal.FORMATTED_ADDRESS + " = " + id,
null,
null
)
if (postal != null) {
while (postal.moveToFirst()) {
clientUiState.clientDetails.clientAddress =
postal.getString(postal.getColumnIndex(StructuredPostal.FORMATTED_ADDRESS))
Log.d("Address", clientUiState.clientDetails.clientAddress)
}
postal.close()
}
val phones: Cursor? = contentResolver.query(
Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = " + id,
null,
null
)
if (phones != null) {
while (phones.moveToNext()) {
clientUiState.clientDetails.clientPhone =
phones.getString(phones.getColumnIndex(Phone.NUMBER))
Log.d("Number", clientUiState.clientDetails.clientPhone)
}
phones.close()
}
val email: Cursor? = contentResolver.query(
Email.CONTENT_URI,
null,
Email.CONTACT_ID + " = ?",
arrayOf(id),
null
)
if (email != null) {
while (email.moveToNext()) {
clientUiState.clientDetails.clientEmail =
email.getString(email.getColumnIndex(Email.DATA))
Log.d("Email", clientUiState.clientDetails.clientEmail)
}
email.close()
}
}
}
}
)
Also tried ActivityResultContracts.StartActivityForResult(), but same results. I'm still a noob so probably missing something basic, or barking up the completely wrong tree, and have been unable to an answer elsewhere on the net.