0

I am creating the bitmap from a linearlayout with the following code

public static Bitmap loadBitmapFromView(View v ) {

    //Bitmap b = Bitmap.createBitmap( v.getLayoutParams.width, v.getLayoutParams.height, Bitmap.Config.ARGB_8888);
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams.width, v.getLayoutParams.height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    v.draw(c);
    return b;
}

but i can not see the views in it all i can see is a big grey screen

have tried the answer at this question also Android Problems Converting ViewGroup with Children into Bitmap but still not working

My view is being inflated from xml and when i use the code mentioned above i get force close for null pointer exception and if i mention the width and height to some number like 200 , 200 then i am able to see the grey background only not the view in it.

Community
  • 1
  • 1
abhishek
  • 1,434
  • 7
  • 39
  • 71

1 Answers1

1

You need to measure the view before layout like

v.measure(
      MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,MeasureSpec.EXACTLY),
      MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight()
        ,Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
v.draw(c);
blessanm86
  • 31,439
  • 14
  • 68
  • 79
  • m getting a nullpointer exception on MeasureSpec.makeMeasureSpec(v.getLayoutParams().width,MeasureSpec.EXACTLY) – abhishek Nov 10 '11 at 08:38
  • well you'll need to find which variable is bull. most probably 'v' will be null. – blessanm86 Nov 10 '11 at 08:40
  • the problem was that i wasnt mentioning the width and height of the linearlayout in java but in xml now it is working thanks a lot – abhishek Nov 10 '11 at 09:40