Load the given image data into the WebView to show the image. - Android User Interface

Android examples for User Interface:WebView Image

Description

Load the given image data into the WebView to show the image.

Demo Code


import android.app.Activity;
import android.util.Log;
import android.view.Display;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class Main{
    private static final String TAG = WebViewUtils.class.getSimpleName();
    /**/*from w  w  w  .  jav a2s .  c  om*/
     * Load the given image data into the WebView to show the image.
     * @param webview
     * @param imageData
     */
    public static void loadImage(WebView webview, byte[] imageData) {
        final String image64 = android.util.Base64.encodeToString(
                imageData, android.util.Base64.DEFAULT);
        String html = "<html><body style='margin:0;padding:0;'><img src=\"data:image/jpeg;base64,"
                + image64 + "\" /></body></html>";
        webview.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
    }
    /**
     * Load the given image file into the WebView to show the image.
     * @param webview
     * @param imageData
     */
    public static void loadImage(WebView webview, String imagePath) {
        webview.getSettings().setAllowFileAccess(true);
        imagePath = "file://" + imagePath;
        Log.d(TAG, "loadDiskImage " + imagePath);
        String html = "<html><body style='margin:0;padding:0;'><img src=\""
                + imagePath + "\"></body></html>";
        webview.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
    }
}

Related Tutorials