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
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);
Try
Bitmap.createBitmap(Bitmap.CompressFormat.PNG, 100, bbb, Bitmap.Config.ARGB_8888);
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With