代码之家  ›  专栏  ›  技术社区  ›  Ticherhaz FreePalestine אור מזרחי

从下载管理器打开PDF文件

  •  0
  • Ticherhaz FreePalestine אור מזרחי  · 技术社区  · 2 年前

    我正在使用 DownloadManager 从下载文件 FirebaseStorage

    首先,我会得到 downloadUrl 从…起 FirebaseStorage 并继续 下载管理器

    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显示 url folderName 价值

    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. 如您所见,代码如下 onReceive 从…起 BroadcastReceiver 处理。
    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>
    

    我试图通过下载管理器的通知打开该文件。它运行良好。

    DownloadManager's Notification

    下图显示了文件在MBITION文件夹中的位置。

    Location file from MBITION folder

    以下是我从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的建议。我试图删除 file:// 通过使用 replace 如下所示:

            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阅读器,几毫秒后,它把我踢回了应用程序。

    0 回复  |  直到 2 年前
        1
  •  1
  •   Ticherhaz FreePalestine אור מזרחי    2 年前

    根据@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)