Programming in kotlin and have created a function creating the CSV-file from the database. When i call the intent with the CSV file the program crashes
This is the CSV file creating function
fun exportToCSV(logItemsAndLogCategory: List<LogCategoryWithLogItems>) : File {
val SEPERATOR = ","
val logFile = File.createTempFile("log_items", "csv")
var fileWriter = FileWriter("log_items.csv")
logItemsAndLogCategory.forEach {
val line = StringBuffer()
fileWriter.append(it.logCategory.name.toString())
fileWriter.append(SEPERATOR)
fileWriter.append(it.logCategory.unit.toString())
fileWriter.append(SEPERATOR)
it.logItems.forEach{
fileWriter.append(it.id.toString())
fileWriter.append(SEPERATOR)
fileWriter.append(it.value.toString())
fileWriter.append(SEPERATOR)
fileWriter.append(it.date.toString())
}
fileWriter.write(line.toString())
fileWriter.write(System.getProperty("line seperator"))
}
fileWriter.flush()
fileWriter.close()
return logFile
}
This is the code for the button which calls the intent:
binding.shareAction.setOnClickListener {
lifecycleScope.launch {
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sharedViewModel.exportToCSV(sharedViewModel.retrieveAllItemsAndCategories())))
sendIntent.type = "text/csv"
startActivity(Intent.createChooser(sendIntent, "SHARE"))
}
}