reuse a function for adding fullsize contact photos

This commit is contained in:
tibbi 2017-12-26 20:12:17 +01:00
parent 6ac34aa532
commit 8af8d3b8ee
1 changed files with 15 additions and 15 deletions

View File

@ -294,6 +294,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
PHOTO_ADDED, PHOTO_CHANGED -> addPhoto(contact, operations) PHOTO_ADDED, PHOTO_CHANGED -> addPhoto(contact, operations)
PHOTO_REMOVED -> removePhoto(contact, operations) PHOTO_REMOVED -> removePhoto(contact, operations)
} }
activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations) activity.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
true true
} catch (e: Exception) { } catch (e: Exception) {
@ -315,7 +316,6 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
val fullSizePhotoData = bitmapToByteArray(bitmap) val fullSizePhotoData = bitmapToByteArray(bitmap)
bitmap.recycle() bitmap.recycle()
bitmap.recycle()
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply { ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.id) withValue(ContactsContract.Data.RAW_CONTACT_ID, contact.id)
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
@ -323,13 +323,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
operations.add(this.build()) operations.add(this.build())
} }
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contact.id.toLong()) addFullSizePhoto(contact.id.toLong(), fullSizePhotoData)
val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
val fileDescriptor = activity.contentResolver.openAssetFileDescriptor(displayPhotoUri, "rw")
val photoStream = fileDescriptor.createOutputStream()
photoStream.write(fullSizePhotoData)
photoStream.close()
fileDescriptor.close()
} }
return operations return operations
} }
@ -341,6 +335,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
withSelection(selection, selectionArgs) withSelection(selection, selectionArgs)
operations.add(this.build()) operations.add(this.build())
} }
return operations return operations
} }
@ -400,6 +395,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
fullSizePhotoData = bitmapToByteArray(bitmap) fullSizePhotoData = bitmapToByteArray(bitmap)
bitmap.recycle() bitmap.recycle()
ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply { ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).apply {
withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE) withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
@ -418,13 +414,7 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
// fullsize photo // fullsize photo
if (contact.photoUri.isNotEmpty() && fullSizePhotoData != null) { if (contact.photoUri.isNotEmpty() && fullSizePhotoData != null) {
val rawContactId = ContentUris.parseId(results[0].uri) val rawContactId = ContentUris.parseId(results[0].uri)
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId) addFullSizePhoto(rawContactId, fullSizePhotoData)
val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
val fileDescriptor = activity.contentResolver.openAssetFileDescriptor(displayPhotoUri, "rw")
val photoStream = fileDescriptor.createOutputStream()
photoStream.write(fullSizePhotoData)
photoStream.close()
fileDescriptor.close()
} }
true true
@ -434,6 +424,16 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
} }
} }
private fun addFullSizePhoto(contactId: Long, fullSizePhotoData: ByteArray) {
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId)
val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
val fileDescriptor = activity.contentResolver.openAssetFileDescriptor(displayPhotoUri, "rw")
val photoStream = fileDescriptor.createOutputStream()
photoStream.write(fullSizePhotoData)
photoStream.close()
fileDescriptor.close()
}
private fun bitmapToByteArray(bitmap: Bitmap): ByteArray { private fun bitmapToByteArray(bitmap: Bitmap): ByteArray {
var baos: ByteArrayOutputStream? = null var baos: ByteArrayOutputStream? = null
try { try {