android - 使用 API 30 通过 PDF.js 加载 PDF

标签 android pdf android-webview pdf.js android-storage

API 30 引入了与 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.js 的应用程序,首先下载 PDF,然后通过 "file:///android_asset/pdfjs/web/viewer.html?file=${path/to/file}" 将不再有效。强制 setAllowFileAccesstrue 是可能的,但不鼓励。

WebViewAssetLoader 被建议作为替代方案,但它似乎适用于侧重于从 /assets 目录或 /res< 加载本地文件的用例 目录。

问题:

  1. 考虑到 API 30 中添加的限制,如何使用 PDF.js 加载本地 PDF 文件?
  2. 有没有办法使用 WebViewAssetLoader 加载保存在应用程序本地存储中的文件?

最佳答案

您可能想尝试使用 https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.InternalStoragePathHandler 它非常适合加载存储在应用程序的 files 目录中的 pdf。

回答您的问题:

  1. 使用WebViewAssetLoader
  2. 使用InternalStoragePathHandler

关于android - 使用 API 30 通过 PDF.js 加载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65852455/

相关文章:

android - 是不是webview已经获取到文件后调用setDownloadListener onDownloadStart?

android - 如何从适用于 Android 的 Google Places GeoDataApi 解析地址(缺少地址组件)

android - 反向地理编码不显示地址

java - Firebase 云消息传递 : random delay between sending and reception of message?

dictionary - 有什么方法可以将字典连接到 Linux 中的 pdf 阅读器吗?

javascript - 在 safari 中显示 pdf 文档

javascript - 如何在 webview android 中处理 html

android - 如何在 Advanced Task Killer 中显示 Android 应用程序名称而不是包名称?

c# - 在 C# 中从 PDF 文件页面创建图像

android - Fragment 内的 CordovaWebView 返回 NullPointerException