簡體   English   中英

使用 Android 中的下載管理器下載后識別特定文件

[英]Identifying the particular file after downloaded using Download Manager in Android

我正在調用下面的函數來下載一個二進制文件。

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() 方法中,如何識別下載了哪個特定文件?

謝謝。

你有文件的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
}

同時識別您的多個下載的一種方法是跟蹤從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 意圖中的額外意圖,指示剛剛完成的下載的 ID(長)。

暫無
暫無

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

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