Android 操作快速查看 - 文件问题 - 尝试打开远程 pdf 文档

Android Action Quick View - Problem with file - trying to open remote pdf document

我在 Android 应用程序中使用 webview。当用户单击下载 link 时,我正在捕获请求 (shouldOverrideUrlLoading) 并通过 API 请求执行下载 - 因为只有在用户通过身份验证后才能下载。因此我正在使用下面的代码。不知何故,许多文件无法正确打开,快速查看显示错误消息 "Problem with file"(附上屏幕截图)。

设置为i.data的URL可以正常工作(认证和下载也可以在任何浏览器的隐私模式下工作)

override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
        if (url.indexOf("example.com") !== -1 && (url.indexOf(".pdf") !== -1 || url.indexOf(".xlsx") !== -1)) {
            println("trying to override url loading")
            val i = Intent(Intent.ACTION_QUICK_VIEW)
            val preferences = contextOfApplication!!.getSharedPreferences("example", Context.MODE_PRIVATE)
            val email = preferences.getString("email", "")
            val token = preferences.getString("token", "")
            i.data = Uri.parse("http://example.com/api/getfile/" + url.substring(url.lastIndexOf('/')+1) + "?mail=" + URLEncoder.encode(email, "UTF-8") + "&token=" + URLEncoder.encode(token, "UTF-8") + "&relative_path=" + URLEncoder.encode(URL(url).path))
            contextOfActivity!!.startActivity(i)
            return true
        }
        return false
    }

url 可能如下所示:

http://example.com/api/getfile/example.pdf?mail=xyz&token=xyz&relative_path=example.pdf

我在 URL 的末尾添加了文件名(由服务器重写),在 Get 参数之前,以便 Android 可以检测到正确的文件扩展名。

也许 ACTION_QUICK_VIEW 不支持 URL 包含 GET 参数?是否可以绕过此错误或临时下载文件然后打开它?

提前致谢!

我会将 the documentation 解释为 Uri 需要是 content Uri.

此外,http 在 Android 的较新版本中默认被禁止(与 https 相比),因此即使支持 Web URL,http 方案可能不会。

您可能需要 download the content yourself,然后使用 FileProvider 制作一个 content Uri 用于您的 Intent