是否可以访问WebView缓存?

fly*_*610 5 android caching webview browser-cache android-webview

我有一个可以访问网页的WebView。当我离线时是否可以访问例如以前下载的图像?如果可能的话,我该怎么做?

提前致谢。

vor*_*olf 1

是的,您可以,至少对于 Android 2.3 及更低版本。

如果您想查看整个缓存文件夹,它位于此处:

<android cache dir>/<your app package>/cache/webviewCache/
Run Code Online (Sandbox Code Playgroud)

如果您已经知道缓存图像的 URL,则可以获取实际文件,如下所示:

String uri = "http://the url of the image that you need";
String hashCode = String.format("%08x", uri.hashCode());
File file = new File(new File(getCacheDir(), "webviewCache"), hashCode);
// if the call file.exist() returns true, then the file presents in the cache
Run Code Online (Sandbox Code Playgroud)