Skip to content Skip to sidebar Skip to footer

Fit Image In Webview

I am planning to display images from SD card in a webview in order to take advantage of he built in zoom capabilities of webview. However, I am facing an issue with displaying imag

Solution 1:

privateWebView mWebView2;
    mWebView2 = (WebView)findViewById(R.id.webview);
    mWebView2.getSettings().setJavaScriptEnabled(true);
    mWebView2.getSettings().setLoadWithOverviewMode(true);
    mWebView2.getSettings().setUseWideViewPort(true);
    mWebView2.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView2.setScrollbarFadingEnabled(true);
    mWebView2.loadDataWithBaseURL("file:///android_asset/", "<img src=\"banner5.png\" height=\"98%\" width=\"100%\"/>", "text/html", "utf-8", null);

Images are in the assets folder

Solution 2:

If you write your HTML correctly, you don't have to do any "setLoadWithOverviewMode", "setUseWideViewPort" or "setInitialScale". And there is absolutely no reason to enable JavaScript.

This one line worked for me:

webView.loadDataWithBaseURL("file://" + directory, "<img src=\"" + name + "\" width=\"100%\"/>", "text/html", "utf-8", null);

The underlining HTML code is:

<img src=YourImage.png width="100%" />, by not setting a height, its aspect ratio will be kept.

Solution 3:

That did the trick for me:

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

Solution 4:

WebSettingssettings= webView.getSettings();

settings.setUseWideViewPort(true);

settings.setLoadWithOverviewMode(true)

Post a Comment for "Fit Image In Webview"