Android add image to webview from a drawable
You can only do such a thing if our image is inside your /assets
folder. Also, you must load your html with a baseUrl that's inside your assets folder.
You can use WebView.loadUrl()
or WebView.loadDataWithBaseURL()
:
webView.loadUrl("file:///android_asset/file.html");
or
webView.loadDataWithBaseURL("file:///android_asset/", "<img src='file.jpg' />", "text/html", "utf-8", null);
(file.jpg should be inside your assets folder)
Load image from drawable folder to webview
this link : Android add image to webview from a drawable
You can only do such a thing if our image is inside your /assets folder. Also, you must load your html with a baseUrl that's inside your assets folder.
You can use WebView.loadUrl() or WebView.loadDataWithBaseURL():
webView.loadUrl("file:///android_asset/file.html");
or
webView.loadDataWithBaseURL("file:///android_asset/", "", "text/html", "utf-8", null);
(file.jpg should be inside your assets folder)
Too , see this link:
how to load a picture from my resource in webview?
Android: how to retrieve an image from res/drawable and put it into WebView.loadUrl()
Android - adding a image in html webview
Create this directory assets/www
then place your html and image etc inside the www
folder.
And use the following code.
Cherry.java
webview.loadUrl("file:///android_asset/www/htmltest.html");
htmltest.html
<img border="0" src="../res/drawable-hdpi/test.png" alt="nothing" width="304" height="228" />
How to put image from drawable folder at HTML file
You can load your html page from android assets folder like below code.
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/manual.html");
setContentView(webView);
and also you need to make your html like this below .
<html>
<p>
Lorem ipsum dolor sit amet,
</p>
<table>
<tr>
<td>
<img src="file:///android_asset/test_image2.jpg"/>
<img src="file:///android_asset/test_image2.jpg" width="50px" alt="Hi">
<img src="file:///android_res/drawable/test_image.png"/>
<img src="file:///android_res/drawable/test_image.png" />
<img src="file:///android_res/drawable/test_image.png" />
</td>
</tr>
</table>
</html>
Access a drawable resource as an image in a webview
(I assume you copied the code piece from your project.)
There is a typo:
String stringPath = "fie:///android_asset/chat_bubble.png";
should be
String stringPath = "file:///android_asset/chat_bubble.png";
And you didn't closed <img>
tag:
addOn = String.format("<img src=\"%s\" >", stringPath);
should be
addOn = String.format("<img src=\"%s\" />", stringPath);
Android: how to retrieve an image from res/drawable and put it into WebView.loadUrl()
In the end I have done the following:
I had an array of file name extensions of each of the image in resources.
And then I could call the following method with the resource ID and it's file name extension.
protected String getResourceURL(int resourceId, String extension) {
String resName = getResources().getResourceName(resourceId);
int index = resName.indexOf("/");
resName = resName.substring(0, index);
index = resName.indexOf(":");
String resType = resName.substring(index + 1);
StringBuilder link = new StringBuilder(128);
link.append("file:///android_res/");
link.append(resType).append("/");
link.append(getResources().getResourceEntryName(resourceId));
link.append(".").append(extension);
return link.toString();
}
Android WebView Load Drawable Variable
The WebView does it's own rendering and doesn't have a hook for you to drop in a decompressed bitmap.
If you simply want to avoid writing to disk, then you could use WebViewClient.shouldInterceptRequest to return the InputStream
for the image to the WebView (something along the lines of this: https://stackoverflow.com/a/13142705/2977376).
If you want to use the Drawable
directly then the only thing I can think of is to have a "hole" in your HTML (essentially an empty div sized to the size of the image) and then draw that image on top of the WebView yourself. This might be challenging since you'd need to handle scale, translate and clipping in your own code, so I wouldn't recommend it.
how to load a picture from my resource in webview?
you can not access Drawable from application's drawable directory like
mWebView.loadUrl("file://res/drawable/map");
for file from your asset directory of your app package you should use
mWebView.loadUrl("content://"+Context.getResources().getAsset()+"filename");
EDIT: ok put your image in Applications asset directory, then for example write line,
myWebView.loadUrl("file:///android_asset/testimage.jpg");
or use
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file://mnt/sdcard/map.png");
try it
Related Topics
Eclipse Can't Find Android.Support.V4.Widget.Swiperefreshlayout
Security "Crypto" Provider Deprecated in Android N
How to Use Std::Stoul and Std::Stoull in Android
Using Numberpicker Widget with Strings
@Font-Face Not Working in Chrome for Android
Get Android .Apk File Versionname or Versioncode Without Installing APK
Android: Create a Toggle Button with Image and No Text
How to Validate a Url/Website Name in Edittext in Android
Read All Sms from a Particular Sender
Use Dll Files in Android Application
Play-Services-Ads Conflicts with Appcompat
Android 4.0.1 Breaks Webview HTML 5 Local Storage
How to Tell If My Textview Has Been Ellipsized
Kotiln: Pass Data from Adapter to Activity
What Features Do Progressive Web Apps Have VS. Native Apps and Vice-Versa, on Android