I am trying to create a bitmap out of a relative layout that i have created programmatically.
The Realtivelayout is showing as expected but when i try to create a bitmap , it returns illegal argument exception that height and width must be > 0
this is how i am doing it
Bitmap.createBitmap(saveLayout.getWidth(),
saveLayout.getHeight(), Bitmap.Config.ARGB_8888);
Any pointers?
Edit: added this code, too:
ViewTreeObserver viewTreeObserver = saveLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
saveLayout.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
viewWidth = saveLayout.getWidth();
viewHeight = saveLayout.getHeight();
}
});
}
In above given code onGlobalLayout() also never seemed to be called.
If you’re programmatically creating the
saveLayout, you may be asking for its width and height too early. (just an educated guess, because I can’t see the rest of your code)See this explanation
The reason why that createBitmap() call throws that exception is:
So, try to defer creating the
Bitmapuntil after the view appears, or find another way to calculate the width/height.This question has a few options/answers for how to defer this calculation to avoid 0 results