这几天做个老人桌面,那些系统自带的通讯录,通讯录列表头像太小了,老人看不清。于是自己写一个老人专用通讯录,给联系人设置大头贴,还能语言报名,这样老人就很好找到需要打电话的联系人!
?????至于通讯录获取头像,网上有很多方法,但是都太乱了!
简介代码
ContentResolver cr = view.getContext().getContentResolver();Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,Long.parseLong(contact.contact_id));InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);Bitmap photo = BitmapFactory.decodeStream(input);
加载到自己的ImageView
Glide.with(context) ???.load(Uri.parse(item.photo_uri)) ???.into(R.id.iv_photo))
怎么获取通讯录数据
val uri = ContactsContract.CommonDataKinds.Contactables.CONTENT_URIval PHONE_BOOK_LABEL = "phonebook_label";contactList = ArrayList<ContactBean>()val cursor = contentResolver.query( ???uri, ???arrayOf( ???????ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ???????ContactsContract.CommonDataKinds.Phone.NUMBER, ???????ContactsContract.CommonDataKinds.Phone.CONTACT_ID, ???????ContactsContract.CommonDataKinds.Phone.PHOTO_FILE_ID, ???????ContactsContract.CommonDataKinds.Phone.PHOTO_ID, ???????ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI, ???????ContactsContract.CommonDataKinds.Phone.PHOTO_URI, ???????PHONE_BOOK_LABEL ???), ???null, ???null, ???ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY)if (null != cursor) { ???while (cursor.moveTonext()) { ???????val firstChar = ???????????cursor.getString(cursor.getColumnIndex(PHONE_BOOK_LABEL)) ???????val contact_id = ???????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)) ???????val name = ???????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)) ???????val phoneNum = ???????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) ???????val photo_id = ???????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_ID)) ???????val photo_file_id = ???????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_FILE_ID)) ???????val photo_uri = ???????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)) ???????val photo_thumbnail_uri = ??????????cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI)) ???????val contact = ContactBean( ???????????firstChar, ???????????contact_id, ???????????name, ???????????phoneNum, ???????????photo_id, ???????????photo_file_id, ???????????photo_uri, ???????????photo_thumbnail_uri ???????) ???????contactList!!.add(contact) ???} ???cursor.close()
自定义的通讯录实体类:
data class ContactBean( ???val firstChar: String? = "", ???val contact_id: String? = "", ???val name: String? = "", ???val phoneNum: String? = "", ???val photo_id: String? = "", ???val photo_file_id: String? = "", ???val photo_uri: String? = "", ???val photo_thumbnail_uri: String? = "") { ???override fun toString(): String { ???????return "${firstChar},${name},${contact_id},${phoneNum},${photo_id},${photo_file_id},${photo_uri},${photo_thumbnail_uri}n" ???}}