簡體   English   中英

從下載管理器打開 PDF 文件位於下載

[英]Open PDF File from Download Manager Located from Download

我正在使用DownloadManagerFirebaseStorage下載文件。

首先,我將從FirebaseStorage獲取downloadUrl並繼續使用DownloadManager

  1. 正如您在下面看到的代碼,這是我將downloadUrl作為url之后的位置。
downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
val uri = Uri.parse(url)
val request = DownloadManager.Request(uri)

val folderName = File.separator + "MBITION" + File.separator + fileName + fileExtension

name = folderName

Log.i("???", "url: $url")
Log.i("???", "folderName: $folderName")

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(
        Environment.DIRECTORY_DOWNLOADS,
        folderName
 )

 enq = downloadManager!!.enqueue(request)

下面的 Logcat 顯示了urlfolderName值。

I/???: url: https://firebasestorage.googleapis.com/v0/b/mbition-2022.appspot.com/o/note%2F-N2_fRAhJXVshDjQMcrz?alt=media&token=936e1014-6c7a-4f46-89fd-5746eb6a9dbf
I/???: folderName: /MBITION/ICT600 - Chapter 3.pdf
  1. 正如您在下面的代碼中看到的,處理來自BroadcastReceiveronReceive的位置。
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
        val downloadId = it.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0)
        val query = DownloadManager.Query()
        query.setFilterById(downloadId)
        val c: Cursor = downloadManager!!.query(query)
        if (c.moveToFirst()) {
               val columnIndex: Int = c.getColumnIndex(DownloadManager.COLUMN_STATUS)
               if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

               val uriString: String = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
               val file = File(uriString)
               val target = Intent(Intent.ACTION_VIEW)

               Log.i("???", "uriString:: $uriString")
               Log.i("???", "file:: $file")

               val uri = FileProvider.getUriForFile(
                      this@NoteActivity,
                      "$packageName.provider",
                       file
               )

               target.setDataAndType(uri, "application/pdf")
               target.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION

               val intentChooser = Intent.createChooser(target, "Open File")
               try {
                    startActivity(intentChooser)
               } catch (e: ActivityNotFoundException) {
                    Tools.showToast(
                        this@NoteActivity,
                        "You need to download PDF reader"
                    )
                }
            }
         }
   }

下面顯示了 Logcat。

I/???: uriString:: file:///storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-5.pdf
I/???: file:: file:/storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-5.pdf

這是我的provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
</paths>

我試圖通過下載管理器的通知打開文件。 它工作正常。

下載管理器的通知

下圖顯示了文件在 MBITION 文件夾中的位置。

MBITION 文件夾中的位置文件

以下是我從 Logcat 得到的錯誤。

2022-05-28 11:36:44.534 4999-4999/com.aaa.mbition E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.aaa.mbition, PID: 4999
    java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203.pdf
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:800)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:442)
        at com.aaa.mbition.ui.NoteActivity$onCompleteDownloadFile$1$onReceive$1.invoke(NoteActivity.kt:79)

更新:

請參閱@blackapps 的建議。 我嘗試使用如下replace來刪除file://

        val uriString: String = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
        val urlFixer = uriString.replace("file://","")

        val file = File(urlFixer)
        val target = Intent(Intent.ACTION_VIEW)

        Log.i("???", "uriString:: $uriString")
        Log.i("???", "file:: $file")
        Log.i("???", "urlFixer:: $urlFixer")
                        
        val uri = FileProvider.getUriForFile(
               this@NoteActivity,
               "$packageName.provider",
               file
        )

下面是Logcat

I/???: uriString:: file:///storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-9.pdf
I/???: file:: /storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-9.pdf
I/???: urlFixer:: /storage/emulated/0/Download/MBITION/ICT600%20-%20Chapter%203-9.pdf

當我按下OK按鈕時,它會進入 PDF 閱讀器,幾毫秒后,它會將我踢回應用程序。

根據@blackapps 的回答是有效的。 我使用replace修復了它們。

val uriString: String = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))
val urlFixer = uriString.replace("file://", "").replace("%20", " ")
                        
val file = File(urlFixer)
val target = Intent(Intent.ACTION_VIEW)

暫無
暫無

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

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