Android: How to Display a Bitmap in a WebView?

前端 未结 5 1209
长发绾君心
长发绾君心 2020-12-09 21:21

I\'m using a WebView for displaying a map-like image, due to the built-in panning and zooming functionality. However, I need to overlay some other information (markers etc)

相关标签:
5条回答
  • 2020-12-09 21:48

    The answer seems to be: no, displaying a local bitmap object in a webview, without saving it to a file, is not possible.

    0 讨论(0)
  • You dont require Bitmap, just its name is enough. Try following:

    html = "<html><img src=\"" + imageUrl
                            + "\"></html>";
    final WebView webView = (WebView) view
                        .findViewById(R.id.yourWebView);
    wvImageView.getSettings().setJavaScriptEnabled(true);
    wvImageView.loadDataWithBaseURL("", html, "text/html", "utf-8", "");
    

    This should display your image on WebView. If you want to provide zooming facility, try this:

    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setBuiltInZoomControls(true);
    
    0 讨论(0)
  • 2020-12-09 22:04

    You don't need placeholder if u want to load image in webview

    You can directly load the dataurl in the webview. For ex:

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
        String dataURL= "data:image/png;base64," + imgageBase64;
    
    webview.loadUrl(dataURL); //pass the bitmap base64 dataurl in URL parameter
    

    Hope it helps others! :-)

    0 讨论(0)
  • 2020-12-09 22:05

    Using the link from itsrajesh4uguys, I've created this code snippet:

    // Desired Bitmap and the html code, where you want to place it
    Bitmap bitmap = YOUR_BITMAP;
    String html="<html><body><img src='{IMAGE_PLACEHOLDER}' /></body></html>";
    
    // Convert bitmap to Base64 encoded image for web
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    byte[] byteArray = byteArrayOutputStream.toByteArray();
    String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
        String image = "data:image/png;base64," + imgageBase64;
    
    // Use image for the img src parameter in your html and load to webview
    html = html.replace("{IMAGE_PLACEHOLDER}", image);
    webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "");
    
    0 讨论(0)
  • 2020-12-09 22:06

    Normally in webview we can set the images using html tags.. this wont make any issues.

    any way use this link for sample question and answer

    Why Bitmap to Base64 String showing black background on webview in android?

    0 讨论(0)
提交回复
热议问题