I tried the below way but when I fetched all the files in requireContext().filesDir. Download file was not present there.
setDestinationInExternalFilesDir(context, context.filesDir.absolutePath,
fileName)
I also tried using setDestinationUri() it created the below crash.
Here is how I did it then, first of all download manager doesn't allow to download files directly into the private storage of the app.
so I had to use okhttp to do the same thing and it worked fine.
/**
* Method to download the file in to the private storage of app,
* this method won't show any downloading notification neither any toast message on
* download start and download complete.
*/
private fun downloadFileInInternalStorage(link: String, fileName: String) {
val mFileName = fileName.replace(" ", "_")
.replace(Pattern.compile("[.][.]+").toRegex(), ".")
val request = Request.Builder()
.url(link)
.build()
val client = OkHttpClient.Builder()
.build()
client.newCall(request).enqueue(object: Callback {
override fun onResponse(call: Call, response: Response) {
val fileData = response.body?.byteStream()
if (fileData != null) {
try {
context.openFileOutput(mFileName, Context.MODE_PRIVATE).use { output ->
output.write(fileData.readBytes())
}
} catch (e: IOException) {
e.printStackTrace()
}
}
}
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
})
}