【发布时间】:2020-07-23 12:55:05
【问题描述】:
我想在 WebView 中使用 webSetting allowFileAccessFromFileURLs,但不推荐使用 setter。这个参数应该怎么设置。
【问题讨论】:
标签: android webview android-websettings
我想在 WebView 中使用 webSetting allowFileAccessFromFileURLs,但不推荐使用 setter。这个参数应该怎么设置。
【问题讨论】:
标签: android webview android-websettings
显然你现在应该使用WebViewAssetLoader。参考here。
将implementation "androidx.webkit:webkit:x.x.x" 添加到您的build.gradle 文件中,将x.x.x 替换为当前版本(1.3.0 是截至本文发布之日的最新版本)。
并像这样在您设置 WebView(活动、片段等)的任何位置加载它:
final WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
.addPathHandler("/assets/", new AssetsPathHandler(this))
.addPathHandler("/res/", new ResourcesPathHandler(this))
.build();
webView.setWebViewClient(new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
WebResourceRequest request) {
return assetLoader.shouldInterceptRequest(request.getUrl());
}
});
// Assets are hosted under http(s)://appassets.androidplatform.net/assets/... .
// If the application's assets are in the "main/assets" folder this will read the file
// from "main/assets/www/index.html" and load it as if it were hosted on:
// https://appassets.androidplatform.net/assets/www/index.html
webview.loadUrl("https://appassets.androidplatform.net/assets/www/index.html");
【讨论】: