开发者

how to show the html contents to the webview using android

Following is my html content which i want to show in the webview using android sdk. It will displays only

开发者_StackOverflow社区

//Please

But when I put this HTML content into the browser then it shows differently.

<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework


help help with homework homework assignments elementary school high school middle school



// --><font color="#60c000" size="4"><strong>Please!</strong></font>

Please suggest how to resolve this problem

I have another problem that in HTML content there is a tag

<img src="http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif" border="0" />

in this images does not shows.


  1. Use web.loadDataWithBaseURL instead of web.loadData (And don't forget to escape strings where it's needed)
  2. You need to add internet permission to download images and view them in your manifest file.

This example works for me:

public class SimpleMusicStream extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        WebView wv = (WebView) findViewById(R.id.WebView01);        

        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String html = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework" +
                "help help with homework homework assignments elementary school high school middle school" +
                "// --><font color='#60c000' size='4'><strong>Please!</strong></font>" +
                "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif'  />";


        wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
    }

}

And don't forget to add:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

in your AndroidManifest.xml file


Either you can do it as above or put html file into the asset folder and the use it loke this to display it

view.loadUrl("file:///android_asset/FILENAME.html");


for image display you can do it like

 String str= " img src=\"http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif\" alt=\"this is img\"ALIGN=\"right\"/>";
    wv.loadData(str, "text/html", "utf-8");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜