开发者

Using MonoDroid, How do I load a local image as part of an HTML page?

开发者 https://www.devze.com 2023-03-19 07:23 出处:网络
Below is my code to load a local HTML document into a MonoDroid Activity screen: String mimeType = \"text/html\";

Below is my code to load a local HTML document into a MonoDroid Activity screen:

 String mimeType = "text/html";
 String encoding = "utf-8";

 web_view = FindViewById<WebView>(Resource.Id.webview);
 web_view.Settings.JavaScriptEnabled = true;开发者_运维技巧

 Stream myInput = Assets.Open(@"Help.htm");
 StreamReader reader = new StreamReader(myInput);
 string text = reader.ReadToEnd();
 web_view.LoadData(text, "text/html", "UTF-8");

Here is my HTML code that includes a .jpg image.

<h2>Chapter 1</h2>
<p>This chapter explains ba bla bla</p>

<img src="Ptdem.jpg" height="90" style="width: 60px" />

<h2>Chapter 2</h2>
<p>This chapter explains ba bla bla</p>

My questions are:

Where should I put the "jpg" file, i.e. in which folder?

What is the correct way to identify the target for the "img src" ? (I have tried all combinations)

Is there a publish MonoDroid example/sample somewhere?

Thanks in advance, Trey


Try putting your jpg file to Assets folder and specifying src="file:///android_asset/Ptdem.jpg" - this should work.

Also loading a file as an asset from binary is not really necessary - you could just write

web_view.LoadUrl("file:///android_asset/Help.htm");

instead of you first code snippet - in this case image src should just be just image name, as it already is in your HTML sample.

P.S. Triple slash in url is not a typo.

0

精彩评论

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