WebSetting allowFileAccessFromFileURLs is Deprecated in API Level 30
Asked Answered
N

1

5

I wanted to use webSetting allowFileAccessFromFileURLs in WebView but the setter is deprecated. how should I set this parameter.

Nicolette answered 23/7, 2020 at 12:55 Comment(0)
A
6

Apparently you should be using WebViewAssetLoader now. Reference here.

Add an implementation "androidx.webkit:webkit:x.x.x" to your build.gradle file replacing x.x.x with the current version (1.3.0 is the most recent one as of the date of this post).

And load it like this in wherever you're setting your WebView (Activity, Fragment, etc):

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");
Adipocere answered 2/9, 2020 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.