# WebViewAssetLoader.InternalStoragePathHandler

Artifact: [androidx.webkit:webkit](https://developer.android.com/jetpack/androidx/releases/webkit) [View Source](https://cs.android.com/search?q=file:androidx/webkit/WebViewAssetLoader.java+class:androidx.webkit.WebViewAssetLoader.InternalStoragePathHandler) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

*** ** * ** ***


```
public final class WebViewAssetLoader.InternalStoragePathHandler implements WebViewAssetLoader.PathHandler
```

<br />

*** ** * ** ***

Handler class to open files from application internal storage. For more information about android storage please refer to [Android Developers Docs: Data and file storage overview](https://developer.android.com/guide/topics/data/data-storage).

To avoid leaking user or app data to the web, make sure to choose `directory` carefully, and assume any file under this directory could be accessed by any web page subject to same-origin rules.

A typical usage would be like:

```
File publicDir = new File(context.getFilesDir(), "public");
// Host "files/public/" in app's data directory under:
// http://appassets.androidplatform.net/public/...
WebViewAssetLoader assetLoader = new WebViewAssetLoader.Builder()
         .addPathHandler("/public/", new InternalStoragePathHandler(context, publicDir))
         .build();
```

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.InternalStoragePathHandler#InternalStoragePathHandler(android.content.Context,java.io.File)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/File.html directory )` Creates PathHandler for app's internal storage. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/webkit/WebResourceResponse.html` | `@https://developer.android.com/reference/androidx/annotation/WorkerThread https://developer.android.com/reference/androidx/webkit/WebViewAssetLoader.InternalStoragePathHandler#handle(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html path)` Opens the requested file from the exposed data directory. |

## Public constructors

### InternalStoragePathHandler

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/webkit#1.1.0)

```
public InternalStoragePathHandler(
    @NonNull Context context,
    @NonNull File directory
)
```

Creates PathHandler for app's internal storage. The directory to be exposed must be inside either the application's internal data directory `https://developer.android.com/reference/android/content/Context.html#getDataDir()` or cache directory `https://developer.android.com/reference/android/content/Context.html#getCacheDir()`. External storage is not supported for security reasons, as other apps with `https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE` may be able to modify the files.

Exposing the entire data or cache directory is not permitted, to avoid accidentally exposing sensitive application files to the web. Certain existing subdirectories of `https://developer.android.com/reference/android/content/Context.html#getDataDir()` are also not permitted as they are often sensitive. These files are (`"app_webview/"`, `"databases/"`, `"lib/"`, `"shared_prefs/"` and `"code_cache/"`).

The application should typically use a dedicated subdirectory for the files it intends to expose and keep them separate from other files.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | `https://developer.android.com/reference/android/content/Context.html` that is used to access app's internal storage. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/File.html directory` | the absolute path of the exposed app internal storage directory from which files can be loaded. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if the directory is not allowed. |

## Public methods

### handle

```
@WorkerThread
public @NonNull WebResourceResponse handle(@NonNull String path)
```

Opens the requested file from the exposed data directory.

The matched prefix path used shouldn't be a prefix of a real web path. Thus, if the requested file cannot be found or is outside the mounted directory a `https://developer.android.com/reference/android/webkit/WebResourceResponse.html` object with a `null``https://developer.android.com/reference/java/io/InputStream.html` 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 `https://developer.android.com/reference/android/webkit/WebResourceResponse.html` with `null``https://developer.android.com/reference/java/io/InputStream.html` 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 `https://developer.android.com/reference/java/net/URLConnection.html#guessContentTypeFromName(java.lang.String)`. Developers should ensure that files are named using standard file extensions. If the file does not have a recognised extension, `"text/plain"` will be used by default.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html path` | the suffix path to be handled. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/webkit/WebResourceResponse.html` | `https://developer.android.com/reference/android/webkit/WebResourceResponse.html` for the requested file. |