在 pdf.js android webview 中打开本地文件("file" 不支持错误)

Open local file in pdf.js android webview ("file" is not supported error)

我正在尝试遵循 here 中给出的答案。根据评论,在 android webview 中使用 pdf.js 加载本地文件应该可以,但我收到以下错误:

I/chromium: [INFO:CONSOLE(18556)] "Fetch API cannot load file:///android_asset/www/pdfjs/web/tracemonkey.pdf. URL scheme "file" is not supported.", source: file:///android_asset/www/pdfjs/build/pdf.js (18556)
I/chromium: [INFO:CONSOLE(13127)] "Uncaught (in promise) DataCloneError: Failed to execute 'postMessage' on 'Worker': TypeError: Failed to fetch could not be cloned.", source: file:///android_asset/www/pdfjs/build/pdf.js (13127)
    [INFO:CONSOLE(985)] "Uncaught (in promise) Error: An error occurred while loading the PDF.", source: file:///android_asset/www/pdfjs/web/viewer.js (985)

这是我的代码:

class PDFViewerLocalActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.pdfviewer_layout)

        val webView = findViewById(R.id.pdfViewer) as WebView
        webView.settings.javaScriptEnabled = true
        webView.settings.allowFileAccessFromFileURLs = true
        webView.settings.allowUniversalAccessFromFileURLs = true
        webView.settings.builtInZoomControls = true
        webView.webChromeClient = WebChromeClient()

        webView.loadUrl("file:///android_asset/www/pdfjs/web/viewer.html?file=tracemonkey.pdf")

    }
}

好吧,这是一个愚蠢的问题。我已经在清单中添加了以下权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是忘记了 API 23 岁及以上你必须在 运行 时请求许可。所以,只需添加请求权限的代码就可以了。

@TargetApi(23)
fun askPermissions() {
    val permissions = arrayOf("android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE")
    val requestCode = 200
    requestPermissions(permissions, requestCode)
}