【问题标题】:WebSetting allowFileAccessFromFileURLs is Deprecated in API Level 30WebSetting allowFileAccessFromFileURLs 在 API 级别 30 中已弃用
【发布时间】:2020-07-23 12:55:05
【问题描述】:

我想在 WebView 中使用 webSetting allowFileAccessFromFileURLs,但不推荐使用 setter。这个参数应该怎么设置。

【问题讨论】:

    标签: android webview android-websettings


    【解决方案1】:

    显然你现在应该使用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");
    

    【讨论】:

      猜你喜欢
      • 2020-12-22
      • 2018-12-24
      • 2020-11-09
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多