1

I tried the following code to convert the LinearLayout to image:

public static Bitmap loadBitmapFromView(View v) {
    v.measure(
            View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().width, View.MeasureSpec.EXACTLY),
            View.MeasureSpec.makeMeasureSpec(v.getLayoutParams().height, View.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);
    return b;

}

but I have this exception

java.lang.IllegalArgumentException: bitmap size exceeds 32 bits

How I can solve this problem?

BekaKK
  • 2,173
  • 6
  • 42
  • 80
  • Possible duplicate of [How to take screenshot of a layout in android](https://stackoverflow.com/questions/34444410/how-to-take-screenshot-of-a-layout-in-android) – Vladyslav Matviienko Oct 27 '17 at 12:03

4 Answers4

0

Use Bitmap.Config.ARGB_8888 and make sure the resolved width and height are not 0.

Alternatively you can also use getDrawingCache

view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache();
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0
public static Bitmap loadBitmapFromView(View view) {
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    return view.getDrawingCache();
}
Antony
  • 603
  • 6
  • 14
  • java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference @Antony – BekaKK Oct 27 '17 at 12:11
0

Try this:

private Bitmap generateBitmap(LinearLayout view)
    {
        //Provide it with a layout params. It should necessarily be wrapping the
        //content as we not really going to have a parent for it.
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));

        //Pre-measure the view so that height and width don't remain null.
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        //Assign a size and position to the view and all of its descendants
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        //Create the bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
                view.getMeasuredHeight(),
                Bitmap.Config.ARGB_8888);
        //Create a canvas with the specified bitmap to draw into
        Canvas c = new Canvas(bitmap);

        //Render this view (and all of its children) to the given Canvas
        view.draw(c);
        return bitmap;
    }
Mehta
  • 1,228
  • 1
  • 9
  • 27
0

Try this code to get Bitmap from LinearLayout in Android:

public static Bitmap CreateBitmap(View layout) {
    layout.setDrawingCacheEnabled(true);
    layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
    layout.buildDrawingCache(true);
    Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    layout.draw(canvas);
    return bitmap;
}
Saeid Mohammadi
  • 319
  • 1
  • 2
  • 11