fix exporting contacts on sd cards

This commit is contained in:
tibbi 2018-04-13 15:01:29 +02:00
parent b7d7eb5e8a
commit de864f362c
3 changed files with 22 additions and 20 deletions

View File

@ -374,8 +374,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
if (contacts.isEmpty()) {
toast(R.string.no_entries_for_exporting)
} else {
toast(R.string.exporting)
VcfExporter().exportContacts(this, file, contacts as ArrayList<Contact>) {
VcfExporter().exportContacts(this, file, contacts as ArrayList<Contact>, true) {
toast(when (it) {
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed

View File

@ -90,7 +90,7 @@ fun BaseSimpleActivity.shareContacts(contacts: ArrayList<Contact>) {
return
}
VcfExporter().exportContacts(this, file, contacts) {
VcfExporter().exportContacts(this, file, contacts, false) {
if (it == VcfExporter.ExportResult.EXPORT_OK) {
sharePathIntent(file.absolutePath, BuildConfig.APPLICATION_ID)
} else {

View File

@ -6,11 +6,9 @@ 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.showErrorToast
import com.simplemobiletools.commons.extensions.toFileDirItem
import com.simplemobiletools.commons.extensions.writeLn
import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.*
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.helpers.VcfExporter.ExportResult.EXPORT_FAIL
import com.simplemobiletools.contacts.models.Contact
import java.io.BufferedWriter
import java.io.ByteArrayOutputStream
@ -26,14 +24,18 @@ class VcfExporter {
private var contactsExported = 0
private var contactsFailed = 0
fun exportContacts(activity: BaseSimpleActivity, file: File, contacts: ArrayList<Contact>, callback: (result: ExportResult) -> Unit) {
try {
activity.getFileOutputStream(file.toFileDirItem(activity)) {
fun exportContacts(activity: BaseSimpleActivity, file: File, contacts: ArrayList<Contact>, showExportingToast: Boolean, callback: (result: ExportResult) -> Unit) {
activity.getFileOutputStream(file.toFileDirItem(activity), true) {
try {
if (it == null) {
callback(EXPORT_FAIL)
return@getFileOutputStream
}
if (showExportingToast) {
activity.toast(R.string.exporting)
}
it.bufferedWriter().use { out ->
for (contact in contacts) {
out.writeLn(BEGIN_VCARD)
@ -88,16 +90,17 @@ class VcfExporter {
contactsExported++
}
}
}
} catch (e: Exception) {
activity.showErrorToast(e)
}
callback(when {
contactsExported == 0 -> EXPORT_FAIL
contactsFailed > 0 -> EXPORT_PARTIAL
else -> EXPORT_OK
})
} catch (e: Exception) {
activity.showErrorToast(e)
}
callback(when {
contactsExported == 0 -> EXPORT_FAIL
contactsFailed > 0 -> ExportResult.EXPORT_PARTIAL
else -> ExportResult.EXPORT_OK
})
}
}
private fun addBitmap(bitmap: Bitmap, out: BufferedWriter) {