Wai Yan Hein Wai Yan Hein - 1 month ago 17
Android Question

Unable to convert viewgroup into bitmap in android

I am developing an Android app that generates image. Before generate image, user will drag and drop views onto ReativeLayout. Then I need to convert that relative layout with child views into bitmap. So I am adding view dynamically to a relative layout and then convert it into bitmap. But my code giving me error. Please see my code below.

I am adding views to relative layout like this

relativeLayout.add(imageView);
relativeLayout.add(textView);
//other steps and so on


This is my function to convert RelativeLayout into Bitmap

private Bitmap convertTemplateViewToBitmap(RelativeLayout 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 bitmap = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
return bitmap;
}


So I call that button in a button click passing relative layout as parameter. When I click, it throws error.

This is the error message in logcat


FATAL EXCEPTION: main
Process: memegenerator.waiyanhein.com.memegenerator, PID: 19993
java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:775)
at
memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity.convertTemplateViewToBitmap(CreateMemeActivity.java:898)
at
memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity.saveBottomItemClick(CreateMemeActivity.java:285)
at
memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity.access$800(CreateMemeActivity.java:51)
at
memegenerator.waiyanhein.com.memegenerator.CreateMemeActivity$2.onTabSelected(CreateMemeActivity.java:251)
at
com.roughike.bottombar.BottomBar.updateSelectedTab(BottomBar.java:717)
at com.roughike.bottombar.BottomBar.handleClick(BottomBar.java:685)
at com.roughike.bottombar.BottomBar.onClick(BottomBar.java:656)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


So please what is wrong with my code? How can I fix it? How can I convert a viewgroup with childs into a bitmap in Android please? Please help me.

Answer

here is my solution:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

Enjoy :)