Beka
Beka

Reputation: 143

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

Upvotes: 9

Views: 11909

Answers (6)

Aace
Aace

Reputation: 1822

I wish Android would have defaulted to a white background instead. Nevertheless, here's what we use to save PNGs with transparency as JPGs with a white background.

public static byte[] ConvertBitmapToJPGBytes(Bitmap img)
{
    img = ConvertTransparentBackgroundToWhite(img);
    if (img == null)
        return null;

    ByteArrayOutputStream stream = new ByteArrayOutputStream( );
    img.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    img.recycle( );
    return stream.toByteArray( );
}

private static Bitmap ConvertTransparentBackgroundToWhite(Bitmap bm)
{
    if (bm == null)
        return null;
    if (bm.hasAlpha( ))
    {
        try
        {
            Bitmap newBitmap = Bitmap.createBitmap(bm.getWidth( ), bm.getHeight( ), bm.getConfig( ));
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bm, 0, 0, null);
            bm = newBitmap;
        }
        catch (RuntimeException exc)
        {
            return null;
        }
    }
    return bm;
}

Upvotes: 1

user889030
user889030

Reputation: 4754

you should set draw color to white and your font color to black

canvas.drawColor(Color.WHITE);

Upvotes: 0

Abhishek
Abhishek

Reputation: 1261

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

Upvotes: 3

Mohamed Moamen
Mohamed Moamen

Reputation: 306

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

Upvotes: 12

agilob
agilob

Reputation: 6223

Try

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

Upvotes: 3

Anil Meenugu
Anil Meenugu

Reputation: 1431

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?

Upvotes: 0

Related Questions