I wanted to use webSetting
allowFileAccessFromFileURLs
in WebView but the setter is deprecated. how should I set this parameter.
WebSetting allowFileAccessFromFileURLs is Deprecated in API Level 30
Asked Answered
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");
© 2022 - 2024 — McMap. All rights reserved.