Android: load image from local storage to webview - android

I download the image and store in the local storage. I load this image from the html file from local storage.
<img src="file:///data/data/com.example/imagefiles/photo.jpg"/>
I load the html file from WebView.
webView.loadUrl("file:///android_asset/show_download_image.html");
But the image didn't shown. I am sure the downloading image is successful.
I want to know that is it possible to load the image from local storage into the html file or is there any way to load the image from local?
Thanks.

Why not try to load image directly like this:
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/photo.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
webView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
Here the image is in sd card. You can change the code for keeping image in asset also.

check your webview javaScriptEnabled is true?
WebView.getSettings().setJavaScriptEnabled(true);

Related

android Picasso load image from storage

i am using Picasso for load image from web and local storage
for load image from web
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
and for load image from storage i add file:// before path for load image
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
but when file path have utf-8 charchter with file name its not load the image like file:///android_asset/۲۰۱٧.png
any idea how to solve it every time i want to use picaso its must string path not Uri or File because its saved in sqlite as string
Try this code:
String url = "file:///android_asset/۲۰۱٧.png";
URLEncoder.encode(url, "UTF-8");
Picasso.with(context).load(url).into(imageView2);
try doing
URIUtil.encodeQuery(url)
see the issue at:
https://github.com/square/picasso/issues/652

Load remote image URL with loadDataWithBaseURL method of WebView

I need to load the HTML content which includes, remote image src.
So I wrote a string like below
String trl = "<html><body><a id=\"example1\" href=\"http://example.com/android/images/4252054277_f0fa91e026.jpg\"><img alt=\"example1\" src=\"http://example.com/android/images/4252054277_f0fa91e026_m.jpg\" /></a></body></html>";
I then load with the below URL
webview.loadDataWithBaseURL("", trl, "text/html", "UTF-8", "");
It's showing the text i.e "example-1" instead of showing the image from the URL. I double check the location of images too. they are valid.
What am I doing wrong?

WebView how to get local res\drawable-hdpi image path

I have a WebView and I want to add a backgroung Image
String htmlPage = "<HTML><BODY background=\"bgrnd.png\" TEXT=\"black\">";
the problem is how to get the image path "bgrnd.png" which is saved in res"\drawable-hdpi" folder.
Thx.
Put your images into assets folder and you can use like this way into your image tag
<img src="file:///android_asset/images/yourimage.png">
In xml use
android:background="#+drawable/bgrnd.png"
Set
mWebView.setBackgroundColor(0); below your string

Using webView's loadDataWithBaseURL not loading images from sdcard

I would like to set some html content in my webview with some pictures.
I've read on the internet i must use loadDataWithBaseUrl to do such a thing because i need to link the folder of images (basUrl) in order.
My html content nicely loads, i can even ran the javascripts perfectly, but for some reason my images cannot be loaded.
Somewhere i've read there are some security reasons and thats why i cant load images from sd card to webview and some says it can be easily done via loadDataWithBaseUrl, so i really dont know which one is true.
Here is my method i tried, maybe with some mistakes so dont be rude:
I got my html file here:
mnt/sdcard/com.mypackage.myproject/3/3.html
My images are here:
mnt/sdcard/com.mypackage.myproject/3/images/Cover.png
And this is my content loading:
myWebView.loadDataWithBaseURL("file:///mnt/sdcard/com.mypackage.myproject/3", myHtml, "text/html", "utf-8", "");
In my html code:
<img src="images/Cover.png" alt="Cover.png" height="820"/>
So as you see i give the baseUrl, and for some reason the webview cannot load my images.
As many said, this can be a solution:
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file:/"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadData(html, "text/html","utf-8");
BUT, i have 700 different html files and there are many images in many different places... so i cannot modify the html code.
Is there a way to link the image folder to my html files to properly use them as a baseUrl?
You have a syntax error in your loading statement. You have to put : after file
myWebView.loadDataWithBaseURL("file:///mnt/sdcard/com.mypackage.myproject/3", myHtml, "text/html", "utf-8", "");
You don't need to put full path of your image into html.
img src="images/test1.jpg"
Instead
img src=\""+ imagePath + "\"

WebView and scale image

I create webview in code. In this webview I show pictures. Now I have two questions. How I can show image in full size, because my pictures has resolution 1381x1829 and I see only part of this picture. And second. As you can see in code I load pictures from assets, but I want to load pictures from sd card. How I can do that?
This is code:
WebView web = new WebView(getContext());
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("file:///android_asset/lj.png");
addView(web);
this solution works perfectly for me :
webView.setInitialScale(30);
WebSettings webSettings = webView.getSettings();
webSettings.setUseWideViewPort(true);
This method webview.getSettings().setBuiltInZoomControls(true) will let you implement build in zoom control for non-multitletouch screen
You can load image from anywhere from sdcard to webview using this code.
That is if u want to load multiple image from multiple location.
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/Your/Folder";
String imagePath = "file:/"+ base + "/test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadData(html, "text/html","utf-8");
But if u want to load images exist under the same parent folder, this will do the trick
String imagePath = "test.jpg";
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
mWebView.loadDataWithBaseURL("file:///mnt/sdcard/Your/Folder/", html, "text/html","utf-8",null);
BE WARN, if u try to load hi-res image like 1600x1840 to webview, webview WILL reduced image res to maintain memory usage, which result in BAD looking image
Add this to your HTML :
<style type='text/css'>
img {max-width: 100%;height:initial;} div,p,span,a {max-width: 100%;}
</style>
This will make your images scale to fit the screen size.
For remove default padding - don't forget set style:
<html><head></head><body style="padding:0; margin:0;"><img src=""+ gifUrl + ""
width="100%"></body></html>

Categories

Resources