The Android Developer Challenge is back! Submit your idea before December 2.

AssetsPathHandler

class AssetsPathHandler : WebViewAssetLoader.PathHandler
kotlin.Any
   ↳ androidx.webkit.WebViewAssetLoader.AssetsPathHandler

Handler class to open a file from assets directory in the application APK.

Summary

Public constructors

<init>(@NonNull context: Context)

Public methods

WebResourceResponse?
handle(@NonNull path: String)

Opens the requested file from the application's assets directory.

Public constructors

<init>

AssetsPathHandler(@NonNull context: Context)
Parameters
context Context: Context used to resolve assets.

Public methods

handle

@WorkerThread @Nullable fun handle(@NonNull path: String): WebResourceResponse?

Opens the requested file from the application's assets directory.

The matched prefix path used shouldn't be a prefix of a real web path. Thus, if the requested file cannot be found a WebResourceResponse object with a null InputStream will be returned instead of null. This saves the time of falling back to network and trying to resolve a path that doesn't exist. A WebResourceResponse with null InputStream will be received as an HTTP response with status code 404 and no body.

The MIME type for the file will be determined from the file's extension using java.net.URLConnection#guessContentTypeFromName. Developers should ensure that asset files are named using standard file extensions. If the file does not have a recognised extension, "text/plain" will be used by default.

Parameters
path String: the suffix path to be handled.
Return
WebResourceResponse?: WebResourceResponse for the requested file.