下载Manger无法在Android中下载大文件

发布于 2025-01-31 11:54:04 字数 1494 浏览 4 评论 0原文

通过大文件,我的意思是8.5 MB和小文件,是指80kb至1MB。

虽然下载管理器可以下载最多2GB

的文件

但以下代码可以下载小文件,但不能下载大文件。对于大文件,它显示了下载通知,但是一段时间后,该通知中显示了0KB/未知(等待重试)。在logcat中,没有错误或任何形式的消息可以指向此问题。我正在从普通服务()类调用此代码。

    if (PermissionsUtil.hasExternalStoragePermission(context)) {
                if (fileName.isNotBlank() && mimeType.isNotBlank()) {
                    // get download service and enqueue file
                    Toaster.showMessageShort(R.string.download_started)
                    manager?.let {
                        enq = it.enqueue(getDownloadManagerRequest(link, mimeType, fileName, withHeader))
                        context.registerReceiver(downloadCompletionReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
                    }
                }
            }

PS:

我能够使用提供给下载管理器的相同URL下载邮递员中的大文件。

我的网络工作正常,我已经在不同的网络上尝试使用6个不同的手机,所有这些都显示出相同的行为。

在下载(大)文件时尝试听所有事件时,我将获得以下活动

paused_waiting_to_retry

更新

所以我尝试了这些这些测试各种不同尺寸的PDF文件(100MB至1GB) ,他们正在下载没有任何问题。但是8.5MB的URL IM进入API响应正在显示paused_waiting_to_retry消息。

而且我还注意到了一件事,当我在Postman中测试1GB文件时,它开始下载文件后几乎需要半秒钟发送请求,但是当我尝试了8.5mb时,几乎需要更多的东西。请求。

因此,是否有任何可以在下载管理器上设置的“超时”之类的东西,因为我认为可能是这种情况,即URL花费大量时间发送请求,并且在主要时间下,下载Manger的时间不超时。 (只是一个假设)

By large file i mean 8.5 MB and by small files I mean 80KB to 1MB.

while download manager can download files up to 2GB

The below code is able to download small files but not large one. For the large files it shows the downloading notification but then after some time shows 0kb/unknown (waiting for retry) in the notification. And in the logcat there is no error or any kind of message which can point to this problem. I'm calling this code from a normal service() class.

    if (PermissionsUtil.hasExternalStoragePermission(context)) {
                if (fileName.isNotBlank() && mimeType.isNotBlank()) {
                    // get download service and enqueue file
                    Toaster.showMessageShort(R.string.download_started)
                    manager?.let {
                        enq = it.enqueue(getDownloadManagerRequest(link, mimeType, fileName, withHeader))
                        context.registerReceiver(downloadCompletionReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
                    }
                }
            }

PS:

Im able to download large file in the postman using the same URL which is being provided to the download manger.

My network is working fine, I have tried with 6 different phone on different networks, all the showing the same behaviors.

On trying to listen to all the events while the (large) file is getting downloaded I get the below event

PAUSED_WAITING_TO_RETRY

Update

So I tried these test pdf files of various different sizes (100MB to 1GB), they are getting downloaded without any issue. But the 8.5MB whose URL im getting in an API response is showing the PAUSED_WAITING_TO_RETRY message.

And I also noticed one thing, when I tested the 1GB file in postman it just took almost half a second to send request after that it started downloading the file, but when I tried the 8.5MB one it almost takes more then a mintune in sending the request.

So is there anything like "Time Out" which can be set on a download manager because I think that might be the case, that the url is taking lot of time in sending the request and in the main time download manger runs out of time. (Just an assumption)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

携君以终年 2025-02-07 11:54:04

下载管理器只有可用的磁盘空间才有限制。要检查下载状态,您需要使用inqueue(...)方法返回的下载ID。检查此示例

private val statusHandler = Handler(Looper.getMainLooper())
private val statusRunnable = Runnable {
     downloadStatusQuery(downloadId)
}

private fun downloadStatusQuery(id: Long) {

    val downloadQuery = DownloadManager.Query()
    downloadQuery.setFilterById(id)

    val cursor = mDownloadManager.query(downloadQuery)
    Thread {
        cursor.use { cursor ->
            if (cursor.moveToFirst()) {
                downloadStatus(cursor, id)
            }
        }
    }.start()
}

private fun downloadStatus(cursor: Cursor, id: Long) {

    //column for download  status
    val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
    val status = cursor.getInt(columnIndex)
    //column for reason code if the download failed or paused
    val columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
    val reason = cursor.getInt(columnReason)
    var statusText = ""
    var reasonText = ""
    val bytesTotal = cursor.getLong(
        cursor.getColumnIndex(
            DownloadManager.COLUMN_TOTAL_SIZE_BYTES
        )
    )

    when (status) {
        DownloadManager.STATUS_FAILED -> {
            statusText = "STATUS_FAILED"
            when (reason) {
                DownloadManager.ERROR_CANNOT_RESUME -> reasonText = "ERROR_CANNOT_RESUME"
                DownloadManager.ERROR_DEVICE_NOT_FOUND -> reasonText = "ERROR_DEVICE_NOT_FOUND"
                DownloadManager.ERROR_FILE_ALREADY_EXISTS -> reasonText =
                    "ERROR_FILE_ALREADY_EXISTS"
                DownloadManager.ERROR_FILE_ERROR -> reasonText = "ERROR_FILE_ERROR"
                DownloadManager.ERROR_HTTP_DATA_ERROR -> reasonText = "ERROR_HTTP_DATA_ERROR"
                DownloadManager.ERROR_INSUFFICIENT_SPACE -> reasonText =
                    "ERROR_INSUFFICIENT_SPACE"
                DownloadManager.ERROR_TOO_MANY_REDIRECTS -> reasonText =
                    "ERROR_TOO_MANY_REDIRECTS"
                DownloadManager.ERROR_UNHANDLED_HTTP_CODE -> reasonText =
                    "ERROR_UNHANDLED_HTTP_CODE"
                DownloadManager.ERROR_UNKNOWN -> reasonText = "ERROR_UNKNOWN"
            }
          statusHandler.removeCallbacks(statusRunnable)
        }
        DownloadManager.STATUS_PAUSED -> {
            statusText = "STATUS_PAUSED"
            when (reason) {
                DownloadManager.PAUSED_QUEUED_FOR_WIFI -> reasonText = "PAUSED_QUEUED_FOR_WIFI"
                DownloadManager.PAUSED_UNKNOWN -> reasonText = "PAUSED_UNKNOWN"
                DownloadManager.PAUSED_WAITING_FOR_NETWORK -> reasonText =
                    "PAUSED_WAITING_FOR_NETWORK"
                DownloadManager.PAUSED_WAITING_TO_RETRY -> reasonText =
                    "PAUSED_WAITING_TO_RETRY"
            }
            statusHandler.postDelayed(statusRunnable, 2000)
        }
        DownloadManager.STATUS_PENDING -> {
            statusHandler.postDelayed(statusRunnable, 2000)
            statusText = "STATUS_PENDING"
        }
        DownloadManager.STATUS_RUNNING -> {
            statusHandler.postDelayed(statusRunnable, 2000)
            statusText = "STATUS_RUNNING"
        }
        DownloadManager.STATUS_SUCCESSFUL -> {
            statusText = "STATUS_SUCCESSFUL"
            statusHandler.removeCallbacks(statusRunnable)
        }
    }

    Log.i(TAG, "Status $statusText : $reasonText : ID: $id")
}

Download manager has limitation only with available disk space. To check downloading status you need to use download id returned by enqueue(...) method. Check this example

private val statusHandler = Handler(Looper.getMainLooper())
private val statusRunnable = Runnable {
     downloadStatusQuery(downloadId)
}

private fun downloadStatusQuery(id: Long) {

    val downloadQuery = DownloadManager.Query()
    downloadQuery.setFilterById(id)

    val cursor = mDownloadManager.query(downloadQuery)
    Thread {
        cursor.use { cursor ->
            if (cursor.moveToFirst()) {
                downloadStatus(cursor, id)
            }
        }
    }.start()
}

private fun downloadStatus(cursor: Cursor, id: Long) {

    //column for download  status
    val columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)
    val status = cursor.getInt(columnIndex)
    //column for reason code if the download failed or paused
    val columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON)
    val reason = cursor.getInt(columnReason)
    var statusText = ""
    var reasonText = ""
    val bytesTotal = cursor.getLong(
        cursor.getColumnIndex(
            DownloadManager.COLUMN_TOTAL_SIZE_BYTES
        )
    )

    when (status) {
        DownloadManager.STATUS_FAILED -> {
            statusText = "STATUS_FAILED"
            when (reason) {
                DownloadManager.ERROR_CANNOT_RESUME -> reasonText = "ERROR_CANNOT_RESUME"
                DownloadManager.ERROR_DEVICE_NOT_FOUND -> reasonText = "ERROR_DEVICE_NOT_FOUND"
                DownloadManager.ERROR_FILE_ALREADY_EXISTS -> reasonText =
                    "ERROR_FILE_ALREADY_EXISTS"
                DownloadManager.ERROR_FILE_ERROR -> reasonText = "ERROR_FILE_ERROR"
                DownloadManager.ERROR_HTTP_DATA_ERROR -> reasonText = "ERROR_HTTP_DATA_ERROR"
                DownloadManager.ERROR_INSUFFICIENT_SPACE -> reasonText =
                    "ERROR_INSUFFICIENT_SPACE"
                DownloadManager.ERROR_TOO_MANY_REDIRECTS -> reasonText =
                    "ERROR_TOO_MANY_REDIRECTS"
                DownloadManager.ERROR_UNHANDLED_HTTP_CODE -> reasonText =
                    "ERROR_UNHANDLED_HTTP_CODE"
                DownloadManager.ERROR_UNKNOWN -> reasonText = "ERROR_UNKNOWN"
            }
          statusHandler.removeCallbacks(statusRunnable)
        }
        DownloadManager.STATUS_PAUSED -> {
            statusText = "STATUS_PAUSED"
            when (reason) {
                DownloadManager.PAUSED_QUEUED_FOR_WIFI -> reasonText = "PAUSED_QUEUED_FOR_WIFI"
                DownloadManager.PAUSED_UNKNOWN -> reasonText = "PAUSED_UNKNOWN"
                DownloadManager.PAUSED_WAITING_FOR_NETWORK -> reasonText =
                    "PAUSED_WAITING_FOR_NETWORK"
                DownloadManager.PAUSED_WAITING_TO_RETRY -> reasonText =
                    "PAUSED_WAITING_TO_RETRY"
            }
            statusHandler.postDelayed(statusRunnable, 2000)
        }
        DownloadManager.STATUS_PENDING -> {
            statusHandler.postDelayed(statusRunnable, 2000)
            statusText = "STATUS_PENDING"
        }
        DownloadManager.STATUS_RUNNING -> {
            statusHandler.postDelayed(statusRunnable, 2000)
            statusText = "STATUS_RUNNING"
        }
        DownloadManager.STATUS_SUCCESSFUL -> {
            statusText = "STATUS_SUCCESSFUL"
            statusHandler.removeCallbacks(statusRunnable)
        }
    }

    Log.i(TAG, "Status $statusText : $reasonText : ID: $id")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文