0

Simple to do in every system but Android, but I have and app that needs to create a csv file in SHARED EXTERNAL Download folder. Shutdown/restart phone/app and the file persist. Then I need to delete the file (persistent URI somewhere?) before I recreate it with new data. (if exists() delete)

I see all kinds of doc pointing to SAF for this process, but can find no examples anywhere of how to perform this simple process of create/delete. If is can do this on API 28 and it work on 29+ that would be great.

Without SAF I've done this:

fun writeCSV() {
    val CSV_HEADER = "Last,First\n"
    val mockData = "Tester,Joe\n"
    val contentValues = ContentValues()
    
    contentValues.put(MediaStore.Downloads.DISPLAY_NAME, "aTest.csv")
    contentValues.put(MediaStore.Downloads.MIME_TYPE, "text/csv")
    contentValues.put(MediaStore.Downloads.IS_PENDING, true)
    val uri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
    val fileUri = contentResolver.insert(uri, contentValues)
    if (fileUri != null) {
        try {
            val outputStream = contentResolver.openOutputStream(fileUri)
            outputStream!!.write(CSV_HEADER.toByteArray())
            outputStream!!.write(mockData.toByteArray())
            outputStream!!.close()
            contentValues.put(MediaStore.Images.Media.IS_PENDING, false)
            contentResolver.update(fileUri, contentValues, null, null)
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

The problem is when fun is called again I need to delete this file and recreate it with new data because Android creates "aTest (1).csv" instead of overwriting it.

Mr.C
  • 31
  • 3
  • 1
    "I see all kinds of doc pointing to SAF for this process" -- the Storage Access Framework is for letting the user control where on the user's device (or in the user's cloud storage) the user wants your app to put the user's data. That does not seem to be what you want. You are welcome to [try `MediaStore.Downloads`](https://commonsware.com/blog/2020/01/11/scoped-storage-stories-diabolical-details-downloads.html), which might hold up for your scenario. – CommonsWare Jan 20 '21 at 00:49
  • I wonder that you could create it. If you can create a file you can delete it too. – blackapps Jan 20 '21 at 11:03
  • How? Repeat: if you can create a file you can delete it. – blackapps Jan 20 '21 at 12:27
  • @blackapps How can I delete a file that has been created? That is my original question. To say I can (after a crash/restart) is not helpful. Please show how to retrieve the uri of an existing file in a known location with a known name. – Mr.C Jan 20 '21 at 12:35
  • `How can I delete a file that has been created?` Wrong question. It was assumed that you (your app) created the file to begin with. Reread my comments. And if your app created the file then start showing your code. – blackapps Jan 20 '21 at 12:49
  • @CommonsWare Thanks for the sample, but that app never deletes a file. The user will not see this file (usually) or touch it in a recycler view to give me a uri. The data must be placed in an area that the "My Files"/file browser can see. – Mr.C Jan 20 '21 at 12:50
  • "Thanks for the sample, but that app never deletes a file" -- correct. "The user will not see this file (usually) or touch it in a recycler view to give me a uri" -- the point of the sample was to illustrate the use of `MediaStore.Downloads` on Android 10. It was not to solve every aspect of your situation. – CommonsWare Jan 20 '21 at 13:00
  • "The data must be placed in an area that the "My Files"/file browser can see" -- then perhaps rather than forcing it to be `Downloads/`, you could use `ACTION_CREATE_DOCUMENT` and let the user control where on the user's device (or in the user's cloud storage) the user wants your app to put the user's data. You can use `takePersistableUriPermission()` to have long-term access to that document, so you can delete it later. – CommonsWare Jan 20 '21 at 13:01
  • Please don't lose focus on my original question. I need to create and delete a CSV file in the Download folder. Should be simple right? I now show how it was created, works fine, but deleting an existing file is the 2nd part of the process. How does one use takePersistableUriPermission() in the context of my working code sample? – Mr.C Jan 20 '21 at 13:12

0 Answers0