【问题标题】:Identifying the particular file after downloaded using Download Manager in Android使用 Android 中的下载管理器下载后识别特定文件
【发布时间】:2021-10-04 00:38:28
【问题描述】:

我正在调用下面的函数来下载二进制文件。

fun downloadFile(
        baseActivity: Context,
        batteryId: String,
        downloadFileUrl: String?,
        title: String?
    ): Long {
        val directory =
            File(Environment.getExternalStorageDirectory().toString() + "/destination_folder")

        if (!directory.exists()) {
            directory.mkdirs()
        }
        //Getting file extension i.e. .bin, .mp4 , .jpg, .png etc..
        val fileExtension = downloadFileUrl?.substring(downloadFileUrl.lastIndexOf("."))
        val downloadReference: Long
        var objDownloadManager: DownloadManager =
            baseActivity.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
        val uri = Uri.parse(downloadFileUrl)
        val request = DownloadManager.Request(uri)

        //Firmware file name as batteryId and extension
        firmwareFileSubPath = batteryId + fileExtension
        request.setDestinationInExternalPublicDir(
            Environment.DIRECTORY_DOWNLOADS,
            "" + batteryId + fileExtension
        )
        request.setTitle(title)
        downloadReference = objDownloadManager.enqueue(request) ?: 0

        return downloadReference
    }

文件下载后,我会在下面的 onReceive() 广播接收器方法中接收它:

override fun onReceive(context: Context, intent: Intent) {
                if (intent.action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
                    intent.extras?.let {
                        //retrieving the file
                        val downloadedFileId = it.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)
                        val downloadManager =
                            getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                        val uri: Uri = downloadManager.getUriForDownloadedFile(downloadedFileId)
                        viewModel.updateFirmwareFilePathToFirmwareTable(uri)
                    }
                }
            }

我正在一个一个下载文件,想知道下载的是哪个文件。 根据特定的文件下载,我必须更新本地数据库中的条目。

那么,在 onReceive() 方法中,我如何识别下载的是哪个特定文件?

谢谢。

【问题讨论】:

  • if(!directory.mkdirs()) 返回;也显示一个 toast 来通知用户。
  • 你有一个 uri 并定义了文件。没有人需要更多。你为什么要?你有源文件的下载参考。
  • @blackapps 如何查看?请详细说明。
  • 识别下载的一种方法是,当您将下载排入队列时,它会返回您在整个系统中唯一的 ID。因此,当您调用此代码时,您需要将该 ID 保存在某处:objDownloadManager.enqueue(request),然后在接收方法上,您可以使用该 ID 进行比较以识别您的下载。

标签: android file kotlin android-download-manager download-manager


【解决方案1】:

同时识别您的多个下载的一种方法是跟踪从DownloadManager 返回到本地数据库的 ID,当您调用 objDownloadManager.enqueue(request) 时映射到给定条目。

DownloadManager.enquque的文件表明:

排队一个新的下载。一旦下载管理器准备好执行它并且连接可用,下载将自动开始。

因此,如果您将该 ID 映射到给定记录的本地数据库条目,那么在 onReceive() 期间,您可以识别回给定记录。

override fun onReceive(context: Context, intent: Intent) {
            if (intent.action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
                intent.extras?.let {
                    //retrieving the file
                    val downloadedFileId = it.getLong(DownloadManager.EXTRA_DOWNLOAD_ID)
                    // Find same id from db that you stored previously
                    val downloadManager =
                        getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                    val uri: Uri = downloadManager.getUriForDownloadedFile(downloadedFileId)
                    viewModel.updateFirmwareFilePathToFirmwareTable(uri)
                }
            }
        }

这里,it.getLong(DownloadManager.EXTRA_DOWNLOAD_ID) 会返回与之前开始下载并返回 enqueue 相同的 id。

EXTRA_DOWNLOAD_ID 的文档表明:

包含在 ACTION_DOWNLOAD_COMPLETE Intent 中的 Intent extra,指示刚刚完成的下载的 ID(以长形式表示)。

【讨论】:

    【解决方案2】:

    你有文件的Uri,现在只需获取文件名来识别文件,你可以使用以下函数获取文件名

    fun getFileName(uri: Uri): String?  {
        var result: String? = null
        when(uri.scheme){
            "content" -> {
                 val cursor: Cursor? = getContentResolver().query(uri, null, null, null, null)
                 cursor.use {
                     if (it != null && it.moveToFirst()) {
                         result = it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
                     }
                 }
            }
            else -> {
                val lastSlashIndex = uri.path?.lastIndexOf('/')
                if(lastSlashIndex != null && lastSlashIndex != -1) {
                     result = uri.path!!.substring(lastSlashIndex + 1)
                }
            }
        }
        return result
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      相关资源
      最近更新 更多