如何编辑联系人照片
How to Edit Contact Photo
我想使用 Kotlin 将联系人更新为新位图 bmp
作为个人资料图片。我找到了 editing contacts 的文档,但我在意图页面上找不到用于更改照片的字段。我发现的任何堆栈溢出解决方案都涉及奇怪的更新方式,即使开发者页面鼓励使用 intents。更改联系人照片的最佳方式是什么?
首先,永远不要使用 bmp,它是如此巨大的文件,你不想将 bmp 照片放入任何数据库。
所以,现在要将 new 图片插入特定的原始联系人,并假设您手头有一些标准图片文件 (jpeg/png),您可以这个:
val rawContactPhotoUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, yourRawContactId), // note that this must be a RAW-contact-id, not a contact-id
RawContacts.DisplayPhoto.CONTENT_DIRECTORY
) // this is the url the represents a RawContact picture
try {
val bytes = yourPictureFile.toByteArray() // get a byte array from your pic
val fd = context.contentResolver.openAssetFileDescriptor(rawContactPhotoUri, "rw")
val os = fd?.createOutputStream()
os?.write(bytes)
os?.close()
fd?.close()
} catch (e: IOException) {
// Handle the error
}
我想使用 Kotlin 将联系人更新为新位图 bmp
作为个人资料图片。我找到了 editing contacts 的文档,但我在意图页面上找不到用于更改照片的字段。我发现的任何堆栈溢出解决方案都涉及奇怪的更新方式,即使开发者页面鼓励使用 intents。更改联系人照片的最佳方式是什么?
首先,永远不要使用 bmp,它是如此巨大的文件,你不想将 bmp 照片放入任何数据库。
所以,现在要将 new 图片插入特定的原始联系人,并假设您手头有一些标准图片文件 (jpeg/png),您可以这个:
val rawContactPhotoUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, yourRawContactId), // note that this must be a RAW-contact-id, not a contact-id
RawContacts.DisplayPhoto.CONTENT_DIRECTORY
) // this is the url the represents a RawContact picture
try {
val bytes = yourPictureFile.toByteArray() // get a byte array from your pic
val fd = context.contentResolver.openAssetFileDescriptor(rawContactPhotoUri, "rw")
val os = fd?.createOutputStream()
os?.write(bytes)
os?.close()
fd?.close()
} catch (e: IOException) {
// Handle the error
}