show Local Image using WebView - Android User Interface

Android examples for User Interface:WebView Image

Description

show Local Image using WebView

Demo Code


import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ZoomButtonsController;
import java.lang.reflect.Field;

public class Main{
    //w ww  . ja  v a 2s.c om
    public static void showLocalImage(Context mContext,
            final WebView webview, final String imageLocalUrl, int color,
            boolean isAdapterScreenWidth, boolean doubleClickEabled) {

        boolean fileExist = FileUtils.isExists(imageLocalUrl);

        if (fileExist) {
            String bgcolor = ColorUtils.toBrowserColor(color);
            String adapterScreenWidth = "";
            if (isAdapterScreenWidth) {
                adapterScreenWidth = " width:99.9%;";
            }

            String style = "<style>"
                    + "* { margin:0; padding:0; background-color:"
                    + bgcolor + ";  }" +

                    "img { " + adapterScreenWidth
                    + " margin:0; padding:0; }" +

                    "div{" + adapterScreenWidth
                    +
                    "    margin:0; padding:0;"
                    + " }/*width height */" +

                    "table{ height:100%; width:100%; text-align:center;}" +

                    " </style>";

            String body = "    <body>"
                    + "        <div>"
                    + "            <table>"
                    + "                <tr>"
                    + "                    <td>"
                    + "                       <img src=\"file://"
                    + imageLocalUrl
                    + "\""
                    +
                    "                               margin="
                    + 0
                    + "                               padding="
                    + 0
                    +
                    "                           />"
                    + "                    </td>" + "                </tr>"
                    + "            </table>" + "        </div>"
                    + "    </body>" + "";

            String data = style + body;

            webview.loadDataWithBaseURL("file://" + imageLocalUrl, data,
                    "text/html", "utf-8", null);

            webview.setVerticalScrollBarEnabled(false); 
            webview.setHorizontalScrollBarEnabled(false);
            webview.getSettings().setBuiltInZoomControls(true); 
            webview.getSettings().setSupportZoom(true); 
            setZoomControlGone(webview);

            if (doubleClickEabled) {
                webview.getSettings().setUseWideViewPort(true);
                webview.getSettings().setLoadWithOverviewMode(true);
            }
            webview.setSaveEnabled(true);

        }
    }
    
    public static void setZoomControlGone(WebView view) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {//Android 3.0, 
            view.getSettings().setDisplayZoomControls(false);
        } else {

            //Android 3.0(11) :
            //java

            Class classType;
            Field field;
            try {
                classType = WebView.class;
                field = classType
                        .getDeclaredField("mZoomButtonsController");
                field.setAccessible(true);
                ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(
                        view);
                mZoomButtonsController.getZoomControls().setVisibility(
                        View.GONE);
                try {
                    field.set(view, mZoomButtonsController);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
    }
}

Related Tutorials