Search code examples
androidhtmlwebviewandroid-webviewandroid-websettings

How to load an img tag in webView in Android?


I want to load the following HTML code in the Android webview.

<!DOCTYPE html>
<html>
<body style = "text-align:center">

<img src="http://XXXXXXXX.com/books_snaps/UR335/1.jpg" alt="pageNo" 

height="100%" width="100%"> 

</body>
</html>

where http://XXXXXXXX.com/books_snaps/UR335/1.jpg is a image link. I want to load this image in <img> tag of above HTML. How can I embed above HTML code in webview of Android?


Solution

  • String htmlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><img src=\"http://shiaislamicbooks.com/books_snaps/UR335/1.jpg\" alt=\"pageNo\" height=\"100%\" width=\"100%\"></body></html>";
    webview.loadDataWithBaseURL(null,htmlString,"text/html","UTF-8","about:blank");
    

    Also, you need to add the internet permission,

    <uses-permission android:name="android.permission.INTERNET" />