properly export local private photos in .vcf files

This commit is contained in:
tibbi 2018-02-11 23:41:53 +01:00
parent 1be6264cf4
commit 3af55404a2
1 changed files with 25 additions and 16 deletions

View File

@ -11,6 +11,7 @@ import com.simplemobiletools.commons.extensions.showErrorToast
import com.simplemobiletools.commons.extensions.writeLn
import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.*
import com.simplemobiletools.contacts.models.Contact
import java.io.BufferedWriter
import java.io.ByteArrayOutputStream
import java.io.File
@ -55,24 +56,12 @@ 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)
addBitmap(bitmap, out)
}
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("")
if (contact.photo != null) {
addBitmap(contact.photo!!, out)
}
out.writeLn(END_VCARD)
@ -91,6 +80,26 @@ class VcfExporter {
})
}
private fun addBitmap(bitmap: Bitmap, out: BufferedWriter) {
val firstLine = "$PHOTO;$ENCODING=$BASE64;$JPEG:"
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("")
}
private fun getNames(contact: Contact): String {
var result = ""
var firstName = contact.firstName