Load HTML code in ANDROID BROWSER

2019-04-13 15:19发布

String url = "<html><body>hello World</body></html>";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

how can i load the HTML source code in the browser? Please kindly explain with an example.

1条回答
男人必须洒脱
2楼-- · 2019-04-13 15:54
Intent i = new Intent();

// MUST instantiate android browser, otherwise it won't work
i.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
i.setAction(Intent.ACTION_VIEW);

String html = "<html><body>hello World</body></html>";

// May work without url encoding, but I think is advisable
// URLEncoder.encode replace space with "+", must replace again with %20
String dataUri = "data:text/html," + URLEncoder.encode(html).replaceAll("\\+","%20");
i.setData(Uri.parse(dataUri));

startActivity(i);
查看更多
登录 后发表回答