@@ -11,6 +11,7 @@ import com.simplemobiletools.commons.extensions.showErrorToast
1111import com.simplemobiletools.commons.extensions.writeLn
1212import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.*
1313import com.simplemobiletools.contacts.models.Contact
14+ import java.io.BufferedWriter
1415import java.io.ByteArrayOutputStream
1516import java.io.File
1617
@@ -55,24 +56,12 @@ class VcfExporter {
5556 }
5657
5758 if (contact.thumbnailUri.isNotEmpty()) {
58- val firstLine = " $PHOTO ;$ENCODING =$BASE64 ;$JPEG :"
5959 val bitmap = MediaStore .Images .Media .getBitmap(activity.contentResolver, Uri .parse(contact.thumbnailUri))
60- val byteArrayOutputStream = ByteArrayOutputStream ()
61- bitmap.compress(Bitmap .CompressFormat .JPEG , 85 , byteArrayOutputStream)
62- bitmap.recycle()
63- val byteArray = byteArrayOutputStream.toByteArray()
64- val encoded = Base64 .encodeToString(byteArray, Base64 .NO_WRAP )
65-
66- val encodedFirstLineSection = encoded.substring(0 , ENCODED_PHOTO_LINE_LENGTH - firstLine.length)
67- out .writeLn(firstLine + encodedFirstLineSection)
68- var curStartIndex = encodedFirstLineSection.length
69- do {
70- val part = encoded.substring(curStartIndex, Math .min(curStartIndex + ENCODED_PHOTO_LINE_LENGTH - 1 , encoded.length))
71- out .writeLn(" $part " )
72- curStartIndex + = ENCODED_PHOTO_LINE_LENGTH - 1
73- } while (curStartIndex < encoded.length)
74-
75- out .writeLn(" " )
60+ addBitmap(bitmap, out )
61+ }
62+
63+ if (contact.photo != null ) {
64+ addBitmap(contact.photo!! , out )
7665 }
7766
7867 out .writeLn(END_VCARD )
@@ -91,6 +80,26 @@ class VcfExporter {
9180 })
9281 }
9382
83+ private fun addBitmap (bitmap : Bitmap , out : BufferedWriter ) {
84+ val firstLine = " $PHOTO ;$ENCODING =$BASE64 ;$JPEG :"
85+ val byteArrayOutputStream = ByteArrayOutputStream ()
86+ bitmap.compress(Bitmap .CompressFormat .JPEG , 85 , byteArrayOutputStream)
87+ bitmap.recycle()
88+ val byteArray = byteArrayOutputStream.toByteArray()
89+ val encoded = Base64 .encodeToString(byteArray, Base64 .NO_WRAP )
90+
91+ val encodedFirstLineSection = encoded.substring(0 , ENCODED_PHOTO_LINE_LENGTH - firstLine.length)
92+ out .writeLn(firstLine + encodedFirstLineSection)
93+ var curStartIndex = encodedFirstLineSection.length
94+ do {
95+ val part = encoded.substring(curStartIndex, Math .min(curStartIndex + ENCODED_PHOTO_LINE_LENGTH - 1 , encoded.length))
96+ out .writeLn(" $part " )
97+ curStartIndex + = ENCODED_PHOTO_LINE_LENGTH - 1
98+ } while (curStartIndex < encoded.length)
99+
100+ out .writeLn(" " )
101+ }
102+
94103 private fun getNames (contact : Contact ): String {
95104 var result = " "
96105 var firstName = contact.firstName
0 commit comments