Search code examples
androidscoped-storage

Saving binary files with scoped storage


I have an app where the user can pick and generate bin files, these files must be easily accessible to the user.

Till now, I have been asking the user to create a folder on the SD card and I save all bin files on that folder, the user can manually add new files and those files will show in the file manager of the app.

I don't know how to do this with scooped storage, looks like I can't save in the default directory because this is not easily accessible by the user, and the biggest problem is that this folder is deleted when the app is removed.

This is a big no-no, all files should be kept even if the app is removed, otherwise, my users will get very mad, the bin files are very important to the user even if the app is deleted.


Solution

  • I was able to find a solution for this, I know it's late, but I hope this helps other people.

    First, you need the file URI, you could obtain this, for example, by picking a file with Intent.ACTION_GET_CONTENT

    Then to obtain an OutputStream

    val p: ParcelFileDescriptor = contentResolver.openFileDescriptor(uri, "w") // w: write, r: read
    val fos = BufferedOutputStream(FileOutputStream(p.fileDescriptor))