If I'm loading a WebView with an HTML document directly (no web server) using WebView.loadData() is there a way for that document to reference local images? If so where do I place them, and how do I reference them?
如果我使用WebView.loadData()直接加载带有HTML文档的WebView(没有Web服务器),那么该文档是否可以引用本地图像?如果是这样我在哪里放置它们,我该如何引用它们?
Or alternatively is there a way to easily encode and embed bitmaps into the HTML document?
或者有没有办法轻松编码和嵌入位图到HTML文档?
I need to display local HTML documents that include images and I'd like to avoid setting up a remote server to serve the resources.
我需要显示包含图像的本地HTML文档,并且我想避免设置远程服务器来提供资源。
4 个解决方案
#1
3
use this
用这个
String html = new String();
html = ("<html><BODY ><table style='margin-top:100px;' align='center'><tr><td><img src=\""+your_image_localpath+"\" width=\""+800+"px\" height=\""+800+"px\" ></td></tr></table> </BODY></html>" );
webView1.loadDataWithBaseURL("file:///sdcard/data/data/your_package_name/",
html,
"text/html",
"utf-8",
"");
#2
3
Did you try file:///android_asset/www/image.jpg
as image src
attribute ? If you have the html document as a file, then you can just put the images into a folder next to the file in assets.
你试过file:///android_asset/www/image.jpg作为图像src属性吗?如果您将html文档作为文件,则可以将图像放入资源中文件旁边的文件夹中。
#3
0
You can use base64 encoding if you don't care about the images and they are just part of the html page.
如果你不关心图像,你可以使用base64编码,它们只是html页面的一部分。
There is another thread that explain how to use the base64 encoding Embedding Base64 Images
还有另一个线程解释如何使用base64编码嵌入Base64图像
#4
0
I think this is better and more clean:
我认为这更好,更干净:
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append("<head>");
html.append("</head>");
html.append("<body>");
html.append("<p><img style='width: 100%;' src='your_image.png' /></p>");
html.append("</body></html>");
your_webview.loadDataWithBaseURL("file:///android_asset/", html.toString(), "text/html", "UTF-8", "");
#1
3
use this
用这个
String html = new String();
html = ("<html><BODY ><table style='margin-top:100px;' align='center'><tr><td><img src=\""+your_image_localpath+"\" width=\""+800+"px\" height=\""+800+"px\" ></td></tr></table> </BODY></html>" );
webView1.loadDataWithBaseURL("file:///sdcard/data/data/your_package_name/",
html,
"text/html",
"utf-8",
"");
#2
3
Did you try file:///android_asset/www/image.jpg
as image src
attribute ? If you have the html document as a file, then you can just put the images into a folder next to the file in assets.
你试过file:///android_asset/www/image.jpg作为图像src属性吗?如果您将html文档作为文件,则可以将图像放入资源中文件旁边的文件夹中。
#3
0
You can use base64 encoding if you don't care about the images and they are just part of the html page.
如果你不关心图像,你可以使用base64编码,它们只是html页面的一部分。
There is another thread that explain how to use the base64 encoding Embedding Base64 Images
还有另一个线程解释如何使用base64编码嵌入Base64图像
#4
0
I think this is better and more clean:
我认为这更好,更干净:
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append("<head>");
html.append("</head>");
html.append("<body>");
html.append("<p><img style='width: 100%;' src='your_image.png' /></p>");
html.append("</body></html>");
your_webview.loadDataWithBaseURL("file:///android_asset/", html.toString(), "text/html", "UTF-8", "");