diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt index 49d1115b..bd29cb23 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/Constants.kt @@ -32,7 +32,9 @@ val BDAY = "BDAY:" val ANNIVERSARY = "ANNIVERSARY:" val PHOTO = "PHOTO" val EMAIL = "EMAIL" +val ENCODING = "ENCODING" val BASE64 = "BASE64" +val JPEG = "JPEG" val VERSION_2_1 = "VERSION:2.1" // phone number/email types diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt index 0a92aede..b8c53758 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/helpers/VcfExporter.kt @@ -1,14 +1,21 @@ package com.simplemobiletools.contacts.helpers +import android.graphics.Bitmap +import android.net.Uri import android.provider.ContactsContract.CommonDataKinds +import android.provider.MediaStore +import android.util.Base64 import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.getFileOutputStream import com.simplemobiletools.commons.extensions.writeLn import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.* import com.simplemobiletools.contacts.models.Contact +import java.io.ByteArrayOutputStream import java.io.File -class VcfExporter() { +class VcfExporter { + private val ENCODED_PHOTO_LINE_LENGTH = 74 + enum class ExportResult { EXPORT_FAIL, EXPORT_OK, EXPORT_PARTIAL } @@ -45,6 +52,27 @@ class VcfExporter() { } } + if (contact.thumbnailUri.isNotEmpty()) { + val firstLine = "$PHOTO;$ENCODING=$BASE64;$JPEG:" + val bitmap = MediaStore.Images.Media.getBitmap(activity.contentResolver, Uri.parse(contact.thumbnailUri)) + val byteArrayOutputStream = ByteArrayOutputStream() + bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byteArrayOutputStream) + bitmap.recycle() + val byteArray = byteArrayOutputStream.toByteArray() + val encoded = Base64.encodeToString(byteArray, Base64.NO_WRAP) + + val encodedFirstLineSection = encoded.substring(0, ENCODED_PHOTO_LINE_LENGTH - firstLine.length) + out.writeLn(firstLine + encodedFirstLineSection) + var curStartIndex = encodedFirstLineSection.length + do { + val part = encoded.substring(curStartIndex, Math.min(curStartIndex + ENCODED_PHOTO_LINE_LENGTH - 1, encoded.length)) + out.writeLn(" $part") + curStartIndex += ENCODED_PHOTO_LINE_LENGTH - 1 + } while (curStartIndex < encoded.length) + + out.writeLn("") + } + out.writeLn(END_VCARD) contactsExported++ }