android - 使用 API 30 通过 PDF.js 加载 PDF
问题描述
API 30 在 a 中引入了与文件访问相关的更改WebView
:
/**
* Enables or disables file access within WebView.
* Note that this enables or disables file system access only. Assets and resources
* are still accessible using file:///android_asset and file:///android_res.
* <p class="note">
* <b>Note:</b> Apps should not open {@code file://} URLs from any external source in
* WebView, don't enable this if your app accepts arbitrary URLs from external sources.
* It's recommended to always use
* <a href="{@docRoot}reference/androidx/webkit/WebViewAssetLoader">
* androidx.webkit.WebViewAssetLoader</a> to access files including assets and resources over
* {@code http(s)://} schemes, instead of {@code file://} URLs. To prevent possible security
* issues targeting {@link android.os.Build.VERSION_CODES#Q} and earlier, you should explicitly
* set this value to {@code false}.
* <p>
* The default value is {@code true} for apps targeting
* {@link android.os.Build.VERSION_CODES#Q} and below, and {@code false} when targeting
* {@link android.os.Build.VERSION_CODES#R} and above.
*/
public abstract void setAllowFileAccess(boolean allow);
通过首先下载 PDF 然后通过显示它来使用 PDF.js 的应用程序"file:///android_asset/pdfjs/web/viewer.html?file=${path/to/file}"
将不再工作。强迫setAllowFileAccess
是true
可能的,但不鼓励。
WebViewAssetLoader
建议作为替代方案,但它似乎面向专注于从/assets
目录或/res
目录加载本地文件的用例。
问题:
- 考虑到 API 30 中添加的限制,如何使用 PDF.js 加载本地 PDF 文件?
- 有没有办法用来
WebViewAssetLoader
加载保存在应用程序本地存储中的文件?
解决方案
您可能想尝试使用https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.InternalStoragePathHandler
它非常适合加载存储在应用程序files
目录中的 pdf。
回答您的问题:
推荐阅读
- swift - 通过大数组过滤会导致冻结 - Swift
- javascript - 如何访问二维数组中的项目?
- python - 将表单验证错误传递回 Wagtail 页面模板
- javascript - 如何在 CSS 或 JS 中像素化 Unicode 字符?
- javascript - 从 JSON 数组中获取特定数据
- c++ - 在字符串中查找字符串/从 cin 设置值的另一种方法?
- typescript - 如何在 Jest 中模拟默认导出函数
- python - 如何在 Matplotlib 中更改颜色气泡图
- json - 使用脚本 API 更新库版本号
- javascript - 使用 Strapi API 生成器更新数组中的对象