0

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"))
        }
    }
  • What's the error in logcat? – Jake Lee Jan 04 '22 at 20:45
  • 2022-01-04 21:59:36.129 8157-8157/com.example.openlog E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.openlog, PID: 8157 java.io.FileNotFoundException: log_items.csv: open failed: EROFS (Read-only file system) – Daniel Diamant Jan 04 '22 at 21:00

0 Answers0