Skip to content

WriteAll 0 byte #13

Description

@aydinozkan

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions