simplecsv icon indicating copy to clipboard operation
simplecsv copied to clipboard

WriteAll 0 byte

Open aydinozkan opened this issue 4 years ago • 1 comments

I'm creating a new csv document in device's Downloads folder with below usage:

class ExportRecipientsContract(private val context: Context, private val recipients: List<RecipientItemModel>) :
    ActivityResultContract<Unit, Unit>() {

    override fun createIntent(context: Context, input: Unit) =
        Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
            addCategory(Intent.CATEGORY_OPENABLE)
            type = "text/csv"
            putExtra(Intent.EXTRA_TITLE, "Sample.csv")
        }

    override fun parseResult(resultCode: Int, intent: Intent?) {
        if (resultCode == Activity.RESULT_OK) {
            try {
                intent?.data?.let { uri ->
                    val csvProcessor: CsvProcessor<RecipientItemModel> = CsvProcessor(RecipientItemModel::class.java)
                    csvProcessor.writeAll(File(uri.toString()), recipients, true)
                }
            } catch (e: Exception) {
                Timber.e(e)
            }
        }
    }
}

Most probably I'm doing something wrong because the data does not get written into the file.

But when I write with OutputStream instead of csvProcessor then it works fine

val data = "\"amount\",\"currency\",\"id\",\"name\",\"phoneNumber\",\"reference\"\n" +
                            "\"15000\",\"AED\",\"1\",\"Aydin Mehmet Ozkan\",\"+254504631682\",\"Salary\"\n" +
                            "\"20000\",\"AED\",\"2\",\"Michail Kulaga\",\"+254504631681\",\"Salary\"\n" +
                            "\"23000\",\"AED\",\"3\",\"Jeffrey Tabios\",\"+254504631680\",\"Salary\""

                    val outputStream = context.contentResolver.openOutputStream(uri)

                    outputStream?.let {
                        it.write(data.toByteArray())
                        it.close()
                    }

Could you please have a look.

Best Regards, Aydin.

aydinozkan avatar Jul 09 '20 05:07 aydinozkan

Sorry for the delay. I can't really read that code. Can you make the one that fails more simple?

j256 avatar Jan 02 '21 18:01 j256