I try to save a File with data my app produces to shared storage. I want the file to be:
- persistent after App uninstall
- be of any mime-type
- I would prefer the file to be put in the “Download” directory (special permission required according to here), but other directory is fine also
I tried to find examples here and in the android dock but found old (~obsolete) and could not find an up-to-date one.
From the Android dock i got this snippet:
// Request code for creating a PDF document.
const val CREATE_FILE = 1
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) // this is an ActivityCompat Method, Compose uses ComponentActivity
}
It is not mentioned in which scope (Activity, ViewModel, ApplicationViewModel – guess it musst be placed in ActivityCopat startActivityForResult
belongs to it) to place this code, furthermore it seems to me to not work with compose – start activity for result is an ActivityCompat method, Compose uses ComponentActivity.
Does anyone know where to get or can provide an minimal example how to manage file creation in Android Compose 11+?