WebViewAssetLoader
public
final
class
WebViewAssetLoader
extends Object
| java.lang.Object | |
| ↳ | androidx.webkit.WebViewAssetLoader |
Helper class to load local files including application's static assets and resources using
http(s):// URLs inside a WebView class.
Loading local files using web-like URLs instead of "file://" is desirable as it is
compatible with the Same-Origin policy.
For more context about application's assets and resources and how to normally access them please refer to Android Developer Docs: App resources overview.
This class is expected to be used within
WebViewClient.shouldInterceptRequest(WebView, WebResourceRequest), which is invoked on a different
thread than application's main thread. Although instances are themselves thread-safe (and may be
safely constructed on the application's main thread), exercise caution when accessing private
data or the view system.
Using http(s):// URLs to access local resources may conflict with a real website. This means
that local files should only be hosted on domains your organization owns (at paths reserved
for this purpose) or the default domain reserved for this: appassets.androidplatform.net.
A typical usage would be like:
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");
Summary
Nested classes | |
|---|---|
class |
WebViewAssetLoader.AssetsPathHandler
Handler class to open a file from assets directory in the application APK. |
class |
WebViewAssetLoader.Builder
A builder class for constructing |
class |
WebViewAssetLoader.InternalStoragePathHandler
Handler class to open files from application internal storage. |
interface |
WebViewAssetLoader.PathHandler
A handler that produces responses for a registered path. |
class |
WebViewAssetLoader.ResourcesPathHandler
Handler class to open a file from resources directory in the application APK. |
Constants | |
|---|---|
String |
DEFAULT_DOMAIN
An unused domain reserved for Android applications to intercept requests for app assets. |
Public methods | |
|---|---|
WebResourceResponse
|
shouldInterceptRequest(Uri url)
Attempt to resolve the |
Inherited methods | |
|---|---|
Constants
DEFAULT_DOMAIN
public static final String DEFAULT_DOMAIN
An unused domain reserved for Android applications to intercept requests for app assets.
It is used by default unless the user specified a different domain.
Constant Value: "appassets.androidplatform.net"
Public methods
shouldInterceptRequest
public WebResourceResponse shouldInterceptRequest (Uri url)
Attempt to resolve the url to an application resource or asset, and return
a WebResourceResponse for the content.
This method should be invoked from within
WebViewClient.shouldInterceptRequest(android.webkit.WebView, String).
| Parameters | |
|---|---|
url |
Uri: the URL to process. |
| Returns | |
|---|---|
WebResourceResponse |
WebResourceResponse if the request URL matches a registered URL,
null otherwise.
|
Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2019-12-27 UTC.