2

I want to save a csv file in the Document folder on Android (or Download if Document can't allow this). I have the file content in a variable hence that's not the problem.

Official Documentation use a new activity :

private fun createFile(pickerInitialUri: Uri) {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
    addCategory(Intent.CATEGORY_OPENABLE)
    type = "application/pdf"
    putExtra(Intent.EXTRA_TITLE, "invoice.pdf")

    // Optionally, specify a URI for the directory that should be opened in
    // the system file picker before your app creates the document.
    putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
}
startActivityForResult(intent, CREATE_FILE)

For now I tried the code below but it's in the app specific storage and I can't find it in my document :

val file = File(path, filename)
Timber.w("Filename : ${file.absolutePath}")

try {
    val fileOutPutStream = FileOutputStream(file)
    fileOutPutStream.write(content.toByteArray())
    fileOutPutStream.close()
} catch (e: IOException) {...}
      

The log is : /storage/emulated/0/Android/data/com.example.mypackage/files/Documents/myfile.csv

How should I proceed?

VicMac
  • 21
  • 2

1 Answers1

-1

You do not have to let the user pick something first.

You can directly create your file in Download or Document folder.

Or in other public folders.

blackapps
  • 8,011
  • 2
  • 11
  • 25