I have an url to download a pdf file as a byte array.
I have to store it in a public directory (e.g. Downloads), so it can be easily accessed by the user.
What is the simplest way?
I tried it with Media Store, but failed to find a tutorial of how to pass a byte array to it. And Im a not even sure if Media Store can ever work with non-media files like a pdf file.
Thank you!
The following code should work without needing any permission:
private fun savePdf(filename: String, content: ByteArray) {
try {
val savePath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
Files.createDirectories(savePath.toPath())
val myExternalFile = File(savePath, filename)
val fos = FileOutputStream(myExternalFile)
fos.write(content)
fos.close()
} catch (e: IOException) {
e.printStackTrace()
}
}