WebSetting 的 allowFileAccessFromFileURLs 在 API 级别 30 中已被弃用

4

我想在WebView中使用webSettingallowFileAccessFromFileURLs,但是setter已经过时了。那我应该如何设置这个参数呢?

1个回答

4

现在你应该使用 WebViewAssetLoader。 参考此处

在你的 build.gradle 文件中添加 implementation "androidx.webkit:webkit:x.x.x",将 x.x.x 替换为当前版本(截至本帖发布日期,1.3.0是最新版本)。

并且在设置 WebView(Activity、Fragment 等)的位置,按以下方式加载它:

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");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接