【问题标题】:Why Bitmap to Base64 String showing black background on webview in android?为什么Bitmap to Base64 String在android的webview上显示黑色背景?
【发布时间】:2011-07-05 06:26:31
【问题描述】:

我正在使用通过使用 canvas 将图像组合成 1 的代码。我将该图像显示给 ImageView 它看起来不错。但是,当我尝试将其显示到 WebView 中时,它会显示背景黑色以正确显示该图像。我尝试更改 HTML 中的背景颜色,但它不会更改颜色。或使透明。任何人都可以帮忙吗? Result is here上图是ImageView,下图是WebView。

public class MyBimapTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView img1 = (ImageView) findViewById(R.id.ImageView01);

    img1.setVisibility(View.INVISIBLE);
    Drawable dra1 = img1.getDrawable();
    Bitmap map1 = ((BitmapDrawable) dra1).getBitmap();
    ImageView img2 = (ImageView) findViewById(R.id.ImageView02);
    img2.setVisibility(View.INVISIBLE);
    Drawable dra2 = img2.getDrawable();
    Bitmap map2 = ((BitmapDrawable) dra2).getBitmap();

    // ***
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    map1.compress(Bitmap.CompressFormat.JPEG, 100, baos);

    byte[] b = baos.toByteArray();
    String abc = Base64.encodeBytes(b);

    byte[] byt = null;
    try {
        byt = Base64.decode(abc);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    map1 = BitmapFactory.decodeByteArray(byt, 0, byt.length);

    // ***
    Bitmap map = combineImages(map1, map2);
    ByteArrayOutputStream bbb = new ByteArrayOutputStream();
    map.compress(Bitmap.CompressFormat.JPEG, 100, bbb);

    byte[] bit = bbb.toByteArray();

    String imgToString = Base64.encodeBytes(bit);

    String imgTag = "<img src='data:image/jpg;base64," + imgToString
            + "' align='left' bgcolor='ff0000'/>";

    WebView webView = (WebView) findViewById(R.id.storyView);
    webView.loadData(imgTag, "text/html", "utf-8");
    Drawable end = new BitmapDrawable(map);

    ImageView img3 = (ImageView) findViewById(R.id.ImageView03);
    img3.setImageBitmap(map);
}

public Bitmap combineImages(Bitmap c, Bitmap s) { 
    Bitmap cs = null;
    int width, height = 0;

    width = c.getWidth() + (s.getWidth() / 2);
    height = c.getHeight() + (s.getHeight() / 2);

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);

    comboImage.drawBitmap(c, 0f, 0f, null);
    comboImage.drawBitmap(s, c.getWidth() - (s.getWidth() / 2), c
            .getHeight()
            - (s.getHeight() / 2), null);
    return cs;
}

}

【问题讨论】:

    标签: android html android-layout android-webview


    【解决方案1】:

    JPEG 格式不支持 alpha 透明度,这就是为什么将原始图像转换为JPEG 时透明背景变为黑色的原因。

    改用PNG 格式:

     map1.compress(Bitmap.CompressFormat.PNG, 100, baos); 
    

    String imgTag = "<img src='data:image/png;base64," + imgToString               
        + "' align='left' bgcolor='ff0000'/>";   
    

    【讨论】:

    • 非常感谢...以前使用压缩格式 JPEG。在此处获取更多详细信息developer.android.com/reference/android/graphics/…
    • 完美的答案,直到 compress() 然而,在哪里应用 imgTag?你能帮帮我吗?
    • @VVB:我在答案中输入的两行都是对 OP 发布的代码的修改。在他的代码中,查找行 webView.loadData(imgTag, "text/html", "utf-8"); - 这是将标签添加到正在构建的网页的地方。
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 2018-04-25
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多