我正在尝试使用 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()
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句