java [Android]如何在WebView中显示位图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [Android]如何在WebView中显示位图相关的知识,希望对你有一定的参考价值。

    String html="<html><body><img src='{IMAGE_URL}' /></body></html>";
    Bitmap bitmap = YOUR_BITMAP;

    // 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_URL}", image);
    webview.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", "")

以上是关于java [Android]如何在WebView中显示位图的主要内容,如果未能解决你的问题,请参考以下文章