0

I am trying to display an image from a local assets directory on a ICS Android app.

The png is 40.png it's under appname\assets\40.png The string in string.xml is:

 <string name="p40"><img src="file:///android_asset/40.png"/></string>

And here is the code I use to load the IMG:

              String myhtmlIMG= getResources().getString(R.string.p40);
              webView.loadData(myhtmlIMG,"text/html", null);  

When I run with this I get an exception:

06-11 15:02:04.986: W/System.err(12939):    at libcore.net.http.AbstractHttpInputStream.checkNotClosed(AbstractHttpInputStream.java:68)
06-11 15:02:04.986: W/System.err(12939):    at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:41)
06-11 15:02:04.990: W/System.err(12939):    at java.io.InputStreamReader.read(InputStreamReader.java:244)
06-11 15:02:04.990: W/System.err(12939):    at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
06-11 15:02:04.990: W/System.err(12939):    at java.io.BufferedReader.readLine(BufferedReader.java:354)

However if I was to make an html file in appname\assets\p40.html and include the same tags I would get the image. Problem is I must use dynamic png inserted into a html string so this is not a solution.

Is it my quote handling perhaps?

dymmeh
  • 22,247
  • 5
  • 53
  • 60
user848106
  • 235
  • 1
  • 8
  • 18

2 Answers2

0

It's an escaping problem. Try using CDATA tags:

<string name="p40"><![CDATA[<img src="file:///android_asset/40.png"/>]]></string>
kabuko
  • 36,028
  • 10
  • 80
  • 93
0

If the above doesn't work, escape everything and do Html.fromHTML()

<string name="p40">&lt;img src=\"file:///android_asset/40.png\"/&gt;</string>

Then

          String myhtmlIMG= Html.fromHtml(getResources().getString(R.string.p40));
          webView.loadData(myhtmlIMG,"text/html", null);

-edit- Just remembered the reason the image isn't loading. You have to override the webview load view to make it able to load from your asset folder. Code in this thread.

Displaying Android asset files in a WebView?

Community
  • 1
  • 1
Zaid Daghestani
  • 8,555
  • 3
  • 34
  • 44