Trying to use android's DownloadManager service but always getting a 404 as reason. Here is the code that queues up a request :
val downloadManager = applicationContext.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(uri)
request.setAllowedOverRoaming(true)
request.setAllowedOverMetered(true)
request.setVisibleInDownloadsUi(false)
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
//TODO : do we need to wait here ??
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(baseSessionActivity, arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), 0);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS , File(absoluteURL).name)
headers?.forEach { pair ->
request.addRequestHeader(pair.first, pair.second)
}
return downloadManager.enqueue(request)
and then here is the code that reads the status after i receive a ACTION_DOWNLOAD_COMPLETE intent :
val query = DownloadManager.Query()
query.setFilterById(downloadId)
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val cursor = downloadManager.query(query)
cursor?.let {
if (cursor.moveToFirst()) {
var reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
var reason = cursor.getInt(reasonIndex)
var status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
I do not see the file on disk so the 404 is right. Please let me know if there is anything wrong in my code. Thanks