Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WriteAll 0 byte #13

Open
aydinozkan opened this issue Jul 9, 2020 · 1 comment
Open

WriteAll 0 byte #13

aydinozkan opened this issue Jul 9, 2020 · 1 comment

Comments

@aydinozkan
Copy link

aydinozkan commented Jul 9, 2020

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.

@j256
Copy link
Owner

j256 commented Jan 2, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants