简体   繁体   中英

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. The src tag of the img has values like the following:

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

but WebView renders no image. 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:

/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).

Finally load the html file like this:

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

I think cache directory is not really a safe place to store things. Have you already considered the option to use getExternalStorageDirectory() ?

My app download the contextual help from internet, save all files in getExternalStorageDirectory() + "/help" and open it from a WebView without any problem.

Look at:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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