views:

1478

answers:

3

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

The file I'm having problems with is named:

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

When I pass 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"

I then tried running URLEncoder.encode() on it and got the error page with the URL presented as:

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

Neither of these URLs were able to open the file (and they both look okay to me). Anyone have any ideas?

UPDATE: If I encode the % by hand (using %25, as commonsware.com suggested) then it loads the image, but it tries to parse it as text, not as an image, so I just get a lot of (basically) garbage.

Hosted by imgur.com

Also, referring to the image in an HTML document with a relative URL isn't working (probably because it's not being 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" />
+1  A: 

Try getting rid of the % in the filename. Or, escape it as %25.

CommonsWare
Well sure, but shouldn't `URLEncoder.encode()` have handled that? Isn't that it's whole reason for existing? Anyhow, I tried it and it tries to open it now, but it's just dumping it out as text instead of rendering the image.
fiXedd
Your <img> tag in your update does not have a properly-encoded relative URL, as you have not escaped your % sign as %25. As to why URLEncoder.encode() is not doing that, I have no idea, but AFAIK you cannot have a bare % in a URL. Moreover, since this is an asset, baked in your APK, why are you fussing with troublesome filenames in the first place? Just rename the file to something simple. If *that* fails, confirm you have a valid JPEG image.
CommonsWare
Basically I'm building a port of an iPhone app that will allow my client to dump files into the assets directory, change a couple settings, then recompile to create a "whole new app". I can't predict what characters the filenames will contain and this is specifically from the test set that he provided me. Believe you me, if I could just rename the files I would.
fiXedd
Oh, also, I noticed the bad name in the `<img>` and corrected it... still didn't work. The image opens fine everywhere else (Firefox, Eye of GNOME, GIMP), but just for good measure I opened it in GIMP, changed it a little, then resaved. Still no luck.
fiXedd
Just for fun I went ahead and removed the `%` from the filename and it made no difference... still won't load as an image.
fiXedd
Do other JPEGs work in your assets, and it is just this one failing? Or are JPEGs overall failing?
CommonsWare
I just tried it with JPEGs, PNGs, and GIFs and I get the same results on them all.
fiXedd
Okay, this has diverged into a different topic all together. I think I'm going to open a new question.
fiXedd
A: 

I would guess that WebView only understands text related content types so it faithfully treating your JPG as base64 encoding, decodes and displays resulted gobble-goop as text. I don't really know if it's possible to set content type for WebView but as workaround you can try to throw img tag inside html tag and load resultet page. Also you probably can only use WebView#loadDataWithBaseUrl

DroidIn.net
Turns out it works fine with images... kinda. See my answer if you care.
fiXedd
+3  A: 

Okay, after spending way too long on this, I've figured out what's going on. Basically, if images stored in the assets/ directory contain a space (e.g., " ") in their file name, they won't render as images.

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

works fine. However,

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

just throws a not found error and

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

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

This unexpected behaviour is present on (at least) 1.5, 1.6, and 2.0 and I filed a bug report.

fiXedd
Thanks for clearing this
DroidIn.net