handle exporting base64 encoded contact images

This commit is contained in:
tibbi 2018-01-15 22:09:30 +01:00
parent f729b0de6a
commit eefb6a1dab
2 changed files with 31 additions and 1 deletions

View File

@ -32,7 +32,9 @@ val BDAY = "BDAY:"
val ANNIVERSARY = "ANNIVERSARY:" val ANNIVERSARY = "ANNIVERSARY:"
val PHOTO = "PHOTO" val PHOTO = "PHOTO"
val EMAIL = "EMAIL" val EMAIL = "EMAIL"
val ENCODING = "ENCODING"
val BASE64 = "BASE64" val BASE64 = "BASE64"
val JPEG = "JPEG"
val VERSION_2_1 = "VERSION:2.1" val VERSION_2_1 = "VERSION:2.1"
// phone number/email types // phone number/email types

View File

@ -1,14 +1,21 @@
package com.simplemobiletools.contacts.helpers package com.simplemobiletools.contacts.helpers
import android.graphics.Bitmap
import android.net.Uri
import android.provider.ContactsContract.CommonDataKinds import android.provider.ContactsContract.CommonDataKinds
import android.provider.MediaStore
import android.util.Base64
import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.getFileOutputStream import com.simplemobiletools.commons.extensions.getFileOutputStream
import com.simplemobiletools.commons.extensions.writeLn import com.simplemobiletools.commons.extensions.writeLn
import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.* import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.*
import com.simplemobiletools.contacts.models.Contact import com.simplemobiletools.contacts.models.Contact
import java.io.ByteArrayOutputStream
import java.io.File import java.io.File
class VcfExporter() { class VcfExporter {
private val ENCODED_PHOTO_LINE_LENGTH = 74
enum class ExportResult { enum class ExportResult {
EXPORT_FAIL, EXPORT_OK, EXPORT_PARTIAL 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) out.writeLn(END_VCARD)
contactsExported++ contactsExported++
} }