1

I want to load an image in WebView in android, and I have:

public static final String URL_EMOTION_2 = "/sdcard/kingfisher_ph/e2.gif";
public static final String FILE = "file:/";
// Get the image url
String imagePath = FILE + URL_EMOTION_2;

If I use webView.loadUrl(imagePath); I can see the e2.gif loaded to screen. However, If I try to create an Html String and load to WebView:

String htmlString = "<html><head></head><body><img src=\"" + imagePath + "\" alt=\"My emotion\"/>"</body></html>";
webView.loadData(htmlString, "text/html", "utf-8");

The code above cannot load image, it show only My emotion String! What's wrong with my code? Thanks everyone!

Edit: Thanks, I solved the problem above with webView.loadDataWithBaseURL(...).

However, I have one more problem that occur when I want to reload my webView (because sometimes, I want to change htmlString and reload webView to show what changed), it usually start a blank web page!! How can I disable it? Or is there any other ways to refresh the webViewafter I change the htmlString intermadiately?

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
  • 3
    try to use the method "loadDataWithBaseURL". See question: http://stackoverflow.com/questions/12444368/show-static-html-with-imges-in-webview/12446546#12446546 – Bruno Bieri Sep 17 '12 at 06:54
  • No problem. You can vote up the answer in the other qustion. – Bruno Bieri Sep 17 '12 at 08:10
  • @viperbone can you offer me any suggestion for my edited problem? Thank you very much!! – Kingfisher Phuoc Sep 17 '12 at 08:26
  • May this question answer your problem: http://stackoverflow.com/questions/2563325/is-there-a-better-way-to-refresh-webview see part with: webView.loadUrl( "javascript:window.location.reload( true )" ); – Bruno Bieri Sep 17 '12 at 08:40

0 Answers0