Why bitmap shows black background in android

I wrote code that can convert tablelayout to bitmap.everythink, working perfectly, but my bitmap has a black background, this is 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 is wrong in my code? Why does my bitmap have a black background? if anyone knows a solution please help me thanks

+5
source share
4 answers

JPEG format should have background color. Therefore, when you convert a PNG image to a JPEG icon, replace the transparent background with black.

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

+7
source

Try

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

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

JPEG clearly provides much better compression and smaller image size than PNG. A small size is desirable for optimizing transactions over the network, storage and uploading images. However, when you save the view in JPEG format, the transparent background defaults to black. Therefore, if you want it to be any other color (including white), you must set the background for the representation of this color using the following code in the XML of your layout.

 android:background="@color/whiteColor" 

And you should define your color in colors.xml as below

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

This should help you achieve your desired compression along with your desired visual effects. All the best...

+1
source

JPEG format does not support alpha transparency, so the transparent background becomes black when converting the original image to JPEG.

Use PNG format instead

I found it here: Why does a Bitmap to Base64 String show a black background on a web browser in Android?

0
source

All Articles