Home » Android » Android Development: Using Image From Assets In A WebView's HTML

Android Development: Using Image From Assets In A WebView's HTML

Posted by: admin April 23, 2020 Leave a comment

Questions:

In my app I’m making a basic HTML help document.
I wanted my app’s logo in the HTML img tag itself, but I don’t know how I’d reference to the logo which will be stored in assets.

Is this possible, if so how?

Thanks for the help!

How to&Answers:

Put your Logo into the assets directory eg: assets/logo.png

Then load your html with:

webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", null);

Reference your img like:

<img src="logo.png">

Answer:

Store the images in assets folder:

enter image description here

Read the image in the html from assets with file:///android_asset/

for example:

String sHtmlTemplate = "<html><head></head><body><img src=\"file:///android_asset/img/mybadge.png\"></body></html>";

load inside the WebView:

webView.loadDataWithBaseURL(null, sHtmlTemplate, "text/html", "utf-8",null);

Answer:

dump them into assets folder and just put in the html

<img src="filename.png">

simple as that

Answer:

You can reference assets with this URL syntax:

file:///android_asset/YourAssetFilename

Answer:

Put your Logo into the assets directory
Example : assets/logo.png

Then

String imgData="<img src=home.png>";

webView.loadDataWithBaseURL("file:///android_asset/", imgData, "text/html", "utf-8", null);