简体   繁体   中英

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

I want that the webview display the picture from the resource and html code from the database but only the html code from the database works fine.

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);

I also tried to use an image from the asset folder but that did not work either.

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

You can use loadDataWithBaseURL .

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

The first argument, must point where the image must be.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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