簡體   English   中英

DownloadManager下載進度不可見

[英]DownloadManager download progress not visible

我正在嘗試使用Android的DownloadManager下載文件,並嘗試使用日志語句通過控制台打印下載內容。 雖然文件已正確下載,但我看不到下載進度的日志聲明這是我的代碼

 private fun downloadPdf(fileName: String?, fileExtension: String?, destinationDirectory: String?, url: String?) {
        val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        val uri = Uri.parse(url)
        val request = DownloadManager.Request(uri)
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setDestinationInExternalPublicDir(destinationDirectory, fileName + fileExtension)
        val downloadId = downloadManager.enqueue(request)

        thread {
            val query = DownloadManager.Query()
            query.setFilterById(downloadId)

            val cursor = downloadManager.query(query)

            if(cursor.moveToFirst()){
                val sizeIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)
                val downloadedIndex = cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)
                val size = cursor.getInt(sizeIndex)
                val downloaded = cursor.getInt(downloadedIndex)

                val progress: Long
                if(size != -1){
                    progress = downloaded * 100L / size
                    runOnUiThread {
                        Log.i("pritishsawantprogress",progress.toString())
                    }
                }
            }
        }
    }

任何幫助將不勝感激

  private fun downloadPdf(fileName: String?, fileExtension: String?, destinationDirectory: String?, url: String?) {
        val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        val uri = Uri.parse(url)
        val request = DownloadManager.Request(uri)
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
        request.setDestinationInExternalPublicDir(destinationDirectory, fileName + fileExtension)
        val downloadId = downloadManager.enqueue(request)

        thread {
            var downloading = true
            while (downloading){
                val query = DownloadManager.Query()
                query.setFilterById(downloadId)

                val cursor = downloadManager.query(query)
                if(cursor.moveToFirst()){
                    val bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
                    val bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))

                    if(cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL){
                        downloading = false
                    }

                    val progress = ((bytesDownloaded * 100L)/bytesTotal).toInt()
                    runOnUiThread {
                        Log.i("pritishsawantprogress",progress.toString())
                    }

                    cursor.close()
                }
            }
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM