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()) { if (contacts.isEmpty()) {
toast(R.string.no_entries_for_exporting) toast(R.string.no_entries_for_exporting)
} else { } else {
toast(R.string.exporting) VcfExporter().exportContacts(this, file, contacts as ArrayList<Contact>, true) {
VcfExporter().exportContacts(this, file, contacts as ArrayList<Contact>) {
toast(when (it) { toast(when (it) {
VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful VcfExporter.ExportResult.EXPORT_OK -> R.string.exporting_successful
VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed VcfExporter.ExportResult.EXPORT_PARTIAL -> R.string.exporting_some_entries_failed

View File

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

View File

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