So, I'm trying to implement a download pdf feature for my app. Here is the code where i start the download manager.
val document = documents[i]
val request = DownloadManager.Request(Uri.parse("https://repository.bsi.ac.id/repo/files/256149/download/File_8-Bab-I-Pengenalan-Android.pdf"))
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
.setTitle("Downloading Pdf-Android")
.setMimeType("application/pdf")
.setDescription("File Description")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"Pdf-Android.pdf"
)
val filter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
applicationContext.registerReceiver(DownloadReceiver(), filter)
downloadManager.enqueue(request)
After quite some times, the download launch but failed immediately, and i can't really debug it since the logcat doesn't display anything. I have tried to set the allowed network types into celluar, give the permission for writing external files.
I tried to use the method from the answer, but for some reason i don't get any information in logcat. But the download still unsuccessfull and no log is found on logcat
i already register the broadcast receiver on manifset like this
<receiver
android:name="somename.DownloadReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
It's a bit of an inconvenient situation; however, due to this code, we encounter the DownloadManager.STATUS_PAUSED
error (Reason log is Download failed: 1). I discovered the relevant information from this link: DownloadManager. Despite encountering this error, the documentation pertaining to it is also quite inadequate.
I just added a method like this to detect the cause of the problem.
val downloadId = downloadManager.enqueue(request)
lifecycleScope.launch {
while (true) {
checkDownloadStatus(applicationContext, downloadId)
delay(10000)
}
}
private fun checkDownloadStatus(context: Context?, downloadId: Long) {
val downloadManager = context?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val query = DownloadManager.Query().setFilterById(downloadId)
val cursor = downloadManager.query(query)
if (cursor.moveToFirst()) {
Log.i("DownloadManager", "cursorStarted")
try {
val columnStatusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
val columnReasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
val status = cursor.getInt(columnStatusIndex)
Log.i("DownloadManagerStatus", "$status")
when (status) {
DownloadManager.STATUS_SUCCESSFUL -> {
Log.i("DownloadManagerSuccess", "Download successful")
}
DownloadManager.STATUS_FAILED -> {
val reason = cursor.getInt(columnReasonIndex)
Log.e("DownloadManagerFailed", "Download failed: $reason")
}
DownloadManager.STATUS_PAUSED -> {
val reason = cursor.getInt(columnReasonIndex)
Log.e("DownloadManagerPaused", "Download paused: $reason")
}
else -> {
val reason = cursor.getInt(columnReasonIndex)
Log.e("DownloadManagerUnknown", "unknown : $reason")
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
cursor.close()
}