开发者

call local image in drawable with html (WebView)

This question has been asked few times in forums, but in my code, i can't display开发者_Go百科 my image. I think it's not the right method :

webViewContact.loadData(db.getParametres().get(0).getInformationParam(), "text/html", "utf-8");

getInformationParam() recup the HTML code, like :

<img src=\\"file:///android_asset/logoirdes_apropos.jpg\\"/> <b>Test</b>

My image file is in drawable, how i can display it ?


There are restrictions about the HTML loaded with loadData() can do. Suggest using loadUrl:

webViewContact.loadUrl("file:///android_asset/" + db.getParametres().get(0).getInformationParam())

You can try the following code, and your file will be at: htmlFile. You can certainly do it in UI thread for now, but you might consider to move this to a AsyncTask later in real production if the file is huge.

String directory = Environment.getExternalStoragePublicDirectory("html_cache");
Writer output;
    try {
      directory.mkdir();
      File htmlFile = new File(directory + File.separator + "give_a_name.html");
      String content = db.getParametres().get(0).getInformationParam();
      // assumes default encoding is OK!
      output = new BufferedWriter(new FileWriter(htmlFile));
      output.write( aContents );
    }
    finally {
      output.close();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜