Home » Android » Why Bitmap showing black background in android

Why Bitmap showing black background in android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I wrote some code witch can to convert tablelayout to bitmap.everythink working perfect but, my bitmap has black background
this is a my source code

public Bitmap sendMyData(TableLayout view) {

    Bitmap bitmap = null;

    ByteArrayOutputStream bbb = new ByteArrayOutputStream();
    view.setDrawingCacheEnabled(true);
    view.layout(0, 0, view.getWidth(), view.getHeight());
    view.buildDrawingCache(true);
    bitmap = Bitmap.createBitmap(view.getDrawingCache());
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bbb);
    view.setDrawingCacheEnabled(false);

    return bitmap;

}

what ‘s wrong in my code?why my bitmap has black background?
if anyone knows solution please help me
thanks

How to&Answers:

JPEG format must have background color.So when you convert PNG image or icon to JPEG, replace the transparent background with black color.

convert it as PNG. bitmap.compress(Bitmap.CompressFormat.PNG, 100,
bbb);

Answer:

Try

Bitmap.createBitmap(Bitmap.CompressFormat.PNG, 100, bbb, Bitmap.Config.ARGB_8888);

Answer:

You may have already found the answer to this question but just for benefit of those who are still looking for the answer, here it is.

JPEG obviously gives far better compression and smaller image size than PNG. The small size is desirable in optimizing network, storage and image loading transactions. However when you save a view as JPEG the transparent background defaults to “black” colour. So if you want it to be any other colour (including white), you have to set the background of the view to that colour with the following code in the XML of your layout

android:background="@color/whiteColor"

And you have to define your color in colors.xml as below

    <color name="whiteColor">#FFFFFF</color>

This should help you achieve the desired compression along with the desired visuals. All the Best…

Answer:

The JPEG format does not support alpha transparency, which is why the transparent background becomes black when you convert your original image to JPEG.

Use the PNG format instead

I found it Here : Why Bitmap to Base64 String showing black background on webview in android?