API 30 introduced changes related to file access within 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);
An application utilizing PDF.js by first downloading a PDF and then displaying it via "file:///android_asset/pdfjs/web/viewer.html?file=${path/to/file}"
will no longer work. Forcing setAllowFileAccess
to true
is possible but discouraged.
WebViewAssetLoader
is suggested as an alternative, however it appears to be geared toward use cases focused on loading local files from the /assets
directory or /res
directory.
Questions:
- How can PDF.js be used to load a local PDF file considering the restrictions added in API 30?
- Is there a way to use
WebViewAssetLoader
to load a file saved in the application's local storage?
question from:
https://stackoverflow.com/questions/65852455/loading-pdfs-via-pdf-js-with-api-30 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…