Method 1:
Use the absolute path file: // user // XX // image.png in the HTML file.
Note: This will display the image, but ifProgramAfter the directory is modified, the image cannot be displayed.
Method 2:
Use placeholders in HTML, for example, <image src = "file:/[myimage]" Height = "200" width = "320">
Replace this placeholder when loading HTML text
CodeAs follows:
String htmlpath = path. getfullpath ("docs/detail.html"); // obtain the absolute path of the HTML file based on the relative path of HTML.
String htmltext = file. opentext (htmlpath). readtoend (); // read HTML content
String ImagePath = path. getfullpath ("images/image.png "). replace ("/","//"). replace ("", "% 20"); // read the absolute path of the image and replace the single slash and space.
Htmltext = htmltext. Replace ("[myimage]", ImagePath); // Replace the image placeholder in the HTML content with the correct path.
Webview. loadhtmlstring (htmltext, null); // webview loads HTML content
Note: If there are too many images, processing is very troublesome. You can only write examples and cannot use them.
Method 3:
Use webview. loadhtmlstring (htmltext,Baseurl); // Baseurl: sets the main page content and base URL
In method 3, we only use relative addresses for image addresses in HTML files.
<Image src = "images/image.png" Height = "200" width = "320">
Code:
Nsurl baseurl = new nsurl (path. getfullpath ("."), true); // obtain the root directory path of the program
Webview. loadhtmlstring (htmltext, baseurl); // when this overload method is used, the image path is automatically processed as baseurl + image. SRC, so that the image can be displayed normally.
Note: When using the second method, HTML is easier to process and the program is easier to process.
Summary: The three methods and the third method are optimal.