We can use WebView for loading images from any url or for loading images from drawable folder, assets folder, or resource folder of our app....

Loading images in WebView in android studio

01:45 0 Comments

We can use WebView for loading images from any url or for loading images from drawable folder, assets folder, or resource folder of our app.

1. To load an image from a website in WebView, we can directly write the url of the image in the WebView loadUrl block. Even animated gifs can be loaded directly to WebView.

WebView webview1 loadUrl http://.....jpg


  1.  webView.loadUrl("file:///android_assets/error.html");  


2. We can modify the width and height of this image loaded in WebView by using following code in WebView loadUrl block:

WebView webview1 loadUrl data:text/html,<img width=100% height="" src="(imageUrl)">


  1.  webView.loadUrl("data:text/html,<img width=100% height="" src="file://locahost://neo/logo.png");


Note that in above code image is name of the image and has to be changed accordingly.
Note that animations do not work with this method.

4. Another way to load an image in WebView is to add a sound using sound manager, and then replace it with an image with same name and extension (.mp3) in the.
 asstent folder.

After that the image can be loaded in WebView using following block:

WebView webview1 loadUrl file:///android_res/raw/image


  1.  webView.loadUrl("file:///android_assets/image.png");


7. To enable zooming images or HTML files loaded in WebView, use an add source directly block in onCreate of the project and put following code in it:



  1. WebSettings webSettings = webview1.getSettings();

  2. webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true);

  3. webSettings.setLoadWithOverviewMode(true);

  4. webSettings.setSupportZoom(true);

  5. webSettings.setBuiltInZoomControls(true);

  6. webSettings.setDisplayZoomControls(false);


The code above enables pinch zoom in WebView. It is applicable to anything loaded in WebView and not just the image. It also loads the WebView with overview mode which means the width of the page fits the screen width when loaded initially, but can be zoomed.

8. To remove the ScrollBar of WebView which appears while zooming WebView, use following code in onCreate event using add source directly block:



  1. webview1.setVerticalScrollBarEnabled(false);

  2. webview1.setHorizontalScrollBarEnabled(false);


0 Comments: