繁体   English   中英

如何将图片从资源和HTML加载到Webview?

[英]How to load a picture from the resource and html into webview?

我希望webview显示资源中的图片和数据库中的html代码,但是只有数据库中的html代码才能正常工作。

WebView wv = (WebView) findViewById(R.id.wv);
String city = "lichtenstein";
String img = "<img src =\"file://res/drawable/picture.png\" width=\"100\" height=\"100\" />";
String dbHTML = cursor.getString(cursor.getColumnIndex(Database.COLUMN_HTML));
String webdata = "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />" + img + "<br />" + "<span style=\"color:#064896;font-weight:bold;\">" + city  + " </span>" + dbHTML;
wv.loadData(webdata, "text/html; charset=UTF-8", null);

我也尝试使用资产文件夹中的图像,但这也不起作用。

String img = "<img src =\"file:///android_asset/picture.png\" width=\"100\" height=\"100\" />";

您可以使用loadDataWithBaseURL

String htmlString = <html><body><img src="file.png"></body></html>
myWebView.loadDataWithBaseURL("file:///android_asset/", htmlString, "text/html", "UTF-8", null);

第一个参数必须指向图像必须位于的位置。

资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM