Skip to content Skip to sidebar Skip to footer

Custom View Converting To Bitmap Returning Black Image

I need to create a custom view and then save it to png file into sdcard. Right now I am getting black colour images in sdcard. I couldn't trace out the issue in code. Can anyone pl

Solution 1:

I got it worked by adding this line:

view.layout(0, 0, width, height);

before creating Bitmap using Bitmap.createBitmap

Solution 2:

try the following code to convert a custom view to Bitmap Object:

LinearLayoutlayout= (LinearLayout) findViewById(R.id.layout_main_for_bitmap);
    layout.setDrawingCacheEnabled(true);
    layout.buildDrawingCache();
    Bitmapbitmap= layout.getDrawingCache();

Solution 3:

The fact that LinearLayout was measured by you manually doesn't means that it was layouted (i.e. layout wasn't invoked). Therefore children are not getting layouted, therefore they don't receive correct position on a ViewGroup, therefore they're not visible.

Instead of trying to mimic drawing lifecycle, I would suggest you to call linearLayout.post() on your LinearLayout (if it's added to view hierarchy of course) and inside Runnable invoke your "snapping" workflow.

Alternatively you can invoke layout manually.

Post a Comment for "Custom View Converting To Bitmap Returning Black Image"