1st attempt to get list of contacts on Android Phone. The following code works, and list is populated. Only issue, instead of showing the email address, it shows the phone number. Unable to figure this out.
private fun getContacts() {
arrayList.clear()
val cursor = this.contentResolver
.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
arrayOf(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Photo.PHOTO_URI,
ContactsContract.CommonDataKinds.Email.ADDRESS,
),null,null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY
)
while (cursor!!.moveToNext()) {
val contactID = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.CONTACT_ID))
val contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY))
val contactNumber = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER))
val photo_uri = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI))
val contactEmail = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.ADDRESS))
val obj = ContactModel()
obj.contid=contactID
obj.contname = contactName
obj.contnumber = contactNumber
if (contactEmail != null) {
obj.contemail=contactEmail
} else {
obj.contemail="No Email"
}
if(photo_uri != null){
obj.contPhoto = MediaStore.Images.Media.getBitmap(contentResolver,Uri.parse(photo_uri))
}
arrayList.add(obj)
}
rcvAdapter.notifyDataSetChanged()
cursor.close()
}
Appreciate any help on this.
You can try this solution:
You need to get the contacts list with details with the following code. The following method will return all contacts with name, phone number and email.
fun getContacts(): ArrayList<ContactDO> {
val contacts = ArrayList<ContactDO>()
contResv.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null)?.use { cursor ->
while (cursor.moveToNext()) {
val contact = ContactDO().apply {
name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))
id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID))
// Fetch email
contResv.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
arrayOf(ContactsContract.CommonDataKinds.Email.ADDRESS),
"${ContactsContract.CommonDataKinds.Email.CONTACT_ID} = ?",
arrayOf(id),
null
)?.use { emails ->
email = if (emails.moveToNext()) {
emails.getString(emails.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.ADDRESS))
} else {
"No Email"
}
}
// Fetch phone number
if (cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER)).toInt() > 0) {
contResv.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
arrayOf(ContactsContract.CommonDataKinds.Phone.NUMBER),
"${ContactsContract.CommonDataKinds.Phone.CONTACT_ID} = ?",
arrayOf(id),
null
)?.use { phones ->
if (phones.moveToNext()) {
contactNumber = phones.getString(phones.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER))
}
}
}
}
contacts.add(contact)
}
}
return contacts
}
public ArrayList<ContactDO> getContacts() {
ArrayList<ContactDO> alContacts = new ArrayList<>();
Cursor cursor = contResv.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null) {
try {
while (cursor.moveToNext()) {
ContactDO objContactDO = new ContactDO();
objContactDO.name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
objContactDO.id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// Fetch email
Cursor emails = contResv.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Email.ADDRESS},
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{objContactDO.id},
null
);
if (emails != null) {
try {
if (emails.moveToNext()) {
objContactDO.email = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)
);
} else {
objContactDO.email = "No Email";
}
} finally {
emails.close();
}
}
// Fetch phone number
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor phones = contResv.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{objContactDO.id},
null
);
if (phones != null) {
try {
if (phones.moveToNext()) {
objContactDO.contactNumber = phones.getString(
phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
);
}
} finally {
phones.close();
}
}
}
alContacts.add(objContactDO);
}
} finally {
cursor.close();
}
}
return alContacts;
}
i have found this solution in this link
if you are still in trouble please let me know. Thank you