Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Bitmap showing black background in android

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

like image 237
Beka Avatar asked Jul 24 '15 11:07

Beka


3 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);

like image 187
Mohamed Moamen Avatar answered Oct 16 '22 09:10

Mohamed Moamen


Try

Bitmap.createBitmap(Bitmap.CompressFormat.PNG, 100, bbb, Bitmap.Config.ARGB_8888);
like image 36
agilob Avatar answered Oct 16 '22 09:10

agilob


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...

like image 2
Abhishek Avatar answered Oct 16 '22 09:10

Abhishek