Link to image in assets from HTML does not work

I am trying to display an image in a WebView in an Android application. The image exists in the resources folder of my Android project. The HTML is software built and has this ref image in it:

HTML is loaded into the WebView with:

webView.loadDataWithBaseURL (urlStr, htmlText, "text / html", "utf-8", urlStr);

urlStr = base url needed to search for other resources. htmlText = programmatically generate HTML

WebView loads the HTML, but the image appears as "missing." I have confirmed that the image exists in this place in the assets. However, it does not appear in the WebView.

I have seen numerous publications and manuals that say this should work, but it is not for me. This is on an Android 3.2 tablet. Does anyone know if this only works on some versions of Android? Any thoughts on why this is not working in my case? Thanks.

+4
source share
2 answers

OK, I finally figured it out. Apparently, there is an error somewhere in the Android code where it fails if there is a space in the image path. So:

<img src="file:///android_asset/SkyInfo/images/Deep Sky/M13-Misti.jpg"> 

the image will not be uploaded. If you change it to:

 <img src="file:///android_asset/SkyInfo/images/DeepSky/M13-Misti.jpg"> 

it will work (assuming you renamed the directory).

Note:

  • This is only a problem when reading from assets.
  • It seems to be a problem with Android 4.x. I ran into this problem in versions 4.0.4 and 4.3, but spaces work fine in version 2.3.x.

Come to Google, directory name spaces have been common for 15 years. Get with the program.

+4
source

enter image description here

  webView.loadDataWithBaseURL("file:///android_asset/", sourse, "text/html", "UTF-8", null); 
+3
source

Source: https://habr.com/ru/post/1402767/


All Articles