Am trying to download media file from server to android default Downloads directory,but on storing the media in downloads i got exception java.io.FileNotFoundException: Download/01-21-2022 04:15.jpeg (No such file or directory)
here is my code
val fileName = message.attributes.jsonObject!!.get("fileName")
val contentLength = response.body()!!.contentLength()
val file = File(Environment.DIRECTORY_DOWNLOADS, fileName as String)
responseBody.byteStream().apply {
file.outputStream().use { fileOut ->
var bytesCopied = 0
val buffer = ByteArray(8*1024)
var bytes = read(buffer)
while (bytes >= 0) {
fileOut.write(buffer, 0, bytes)
bytesCopied += bytes
bytes = read(buffer)
reportStatus(((bytesCopied * 100) / contentLength).toInt())
}
}
}