Why can't WebView open some local URLs (Android)?

I have a WebView that I use to open some files stored in the assets/ directory of my project. It works great for most files, but there, in particular (and I'm sure the others I haven't found), it just won't open.

I am having problems with the name:

 "assets/ContentRoot/Photos/XXX Software Logo - jpg - 75%.JPG" 

When I WebView it to WebView and it shows the error page, it shows it as:

 "file:///android_asset/ContentRoot/Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG" 

Then I tried running URLEncoder.encode() on it and got a page with a URL presented as:

 "file:///android_asset/ContentRoot/Photos/XXX+Software+Logo+-+jpg+-+75%.JPG" 

None of these URLs were able to open the file (and they both look good to me). Does anyone have any ideas?

UPDATE: If I coded % manually (using the %25 as proposed commonsware.com), it loads the image, but he is trying to analyze it as text, not as an image, so I just get a lot of (mostly) waste.

Hosted by imgur.com

Also, accessing an image in an HTML document with a relative URL does not work (perhaps because it is not parsed as an image?):

 <img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%.JPG" /> <img src="../Photos/XXX%20Software%20Logo%20-%20jpg%20-%2075%25.JPG" /> 
+4
source share
3 answers

Well, holding out too long, I realized what was happening. In principle, if the images stored in the assets/ directory contain a space (for example, "") in the name of their file, they will not be displayed as images.

 myWebView.loadUrl("file:///android_asset/testimage.jpg"); 

works great. However,

 myWebView.loadUrl("file:///android_asset/test+image.jpg"); 

just throws an error not found and

 myWebView.loadUrl("file:///android_asset/test image.jpg"); // and myWebView.loadUrl("file:///android_asset/test%20image.jpg"); 

show it incorrectly displayed (as text ... see screenshot in question).

This unexpected behavior is present (at least) 1.5, 1.6 and 2.0, and I sent an error report .

+9
source

Try to get rid of % in the file name. Or, avoid it as %25 .

+3
source

I would suggest that WebView only understands content types associated with text, so it correctly treats your JPG as base64 encoding, decodes and displays the resulting gobble-goop as text. I really don't know if it is possible to set the content type for WebView, but as a workaround, you can try loading the img tag inside the html tag and loading the results page. Also you can probably only use WebView # loadDataWithBaseUrl

0
source

All Articles