user1302569
user1302569

Reputation: 7191

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

Upvotes: 0

Views: 3540

Answers (4)

Khalil Kitar
Khalil Kitar

Reputation: 341

this solution works perfectly for me :

webView.setInitialScale(30);
WebSettings webSettings = webView.getSettings();
webSettings.setUseWideViewPort(true);

Upvotes: 3

tier777
tier777

Reputation: 1035

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>

Upvotes: 0

Ronen Yacobi
Ronen Yacobi

Reputation: 844

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.

Upvotes: 1

Hein
Hein

Reputation: 2683

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

Upvotes: 1

Related Questions