fix exporting contacts on sd cards
This commit is contained in:
parent
b7d7eb5e8a
commit
de864f362c
|
@ -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
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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) {
|
||||||
|
activity.getFileOutputStream(file.toFileDirItem(activity), true) {
|
||||||
try {
|
try {
|
||||||
activity.getFileOutputStream(file.toFileDirItem(activity)) {
|
|
||||||
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,17 +90,18 @@ class VcfExporter {
|
||||||
contactsExported++
|
contactsExported++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
activity.showErrorToast(e)
|
activity.showErrorToast(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(when {
|
callback(when {
|
||||||
contactsExported == 0 -> EXPORT_FAIL
|
contactsExported == 0 -> EXPORT_FAIL
|
||||||
contactsFailed > 0 -> EXPORT_PARTIAL
|
contactsFailed > 0 -> ExportResult.EXPORT_PARTIAL
|
||||||
else -> EXPORT_OK
|
else -> ExportResult.EXPORT_OK
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun addBitmap(bitmap: Bitmap, out: BufferedWriter) {
|
private fun addBitmap(bitmap: Bitmap, out: BufferedWriter) {
|
||||||
val firstLine = "$PHOTO;$ENCODING=$BASE64;$JPEG:"
|
val firstLine = "$PHOTO;$ENCODING=$BASE64;$JPEG:"
|
||||||
|
|
Loading…
Reference in New Issue