简体   繁体   English

WebView显示来自本地缓存的图像

[英]WebView showing images from local cache

I have to show some HTML content in WebView which has both images from URL's over the internet and images from the local phone cache. 我必须在WebView中显示一些HTML内容,该内容既包含来自Internet上URL的图像,也包含来自本地电话缓存的图像。 The src tag of the img has values like the following: img的src标记具有如下值:

file:///data/data/com.Domain.MyApp/cache/example.jpg 文件:///data/data/com.Domain.MyApp/cache/example.jpg

but WebView renders no image. 但WebView不会呈现任何图像。 I am sure that the path is correct and that file exists. 我确定路径正确并且该文件存在。 What could be the problem and how can I solve it? 可能是什么问题,我该如何解决?

Set src attributes of img tags like in this example: 设置img标签的src属性,如下例所示:

/data/data/com.Domain.MyApp/cache/example.jpg

Save the HTML content as an HTML file into the same folder with the images (cache directory in the current problem). 将HTML内容另存为与图像相同的文件夹(当前问题中的缓存目录)为HTML文件。

Finally load the html file like this: 最后像这样加载html文件:

mDescriptionWeb.loadUrl("file://" + this.getCacheDir() + "/" + "current.html");

I think cache directory is not really a safe place to store things. 我认为cache目录并不是真正安全的存储位置。 Have you already considered the option to use getExternalStorageDirectory() ? 您是否已经考虑过使用getExternalStorageDirectory()的选项?

My app download the contextual help from internet, save all files in getExternalStorageDirectory() + "/help" and open it from a WebView without any problem. 我的应用程序从Internet下载了上下文帮助,将所有文件保存在getExternalStorageDirectory() + "/help" ,然后从WebView打开它没有任何问题。

Look at: 看着:

http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29 http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29

Give it a try... 试试看...

Another option is to use Assets, look at http://developer.android.com/reference/android/content/res/AssetManager.html 另一个选择是使用资产,请查看http://developer.android.com/reference/android/content/res/AssetManager.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM