Rod Kimble
Rod Kimble

Reputation: 1364

Save Layout as Bitmap

I want to save my Linearlayout as an bmp.

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout_QrCode" >

        <Space
            android:layout_width="15dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <Space
                android:layout_width="match_parent"
                android:layout_height="15dp" />

            <ImageView
                android:layout_width="185dp"
                android:layout_height="185dp"
                android:id="@+id/imageView_qrcode" />

            <Space
                android:layout_width="match_parent"
                android:layout_height="15dp" />

        </LinearLayout>

        <RelativeLayout
            android:layout_width="417dp"
            android:layout_height="match_parent" >

            <Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true"
                android:id="@+id/space2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView_QrSticker_label"
                android:layout_alignParentStart="true"
                android:layout_above="@+id/space2"
                android:textStyle="bold"
                android:textSize="40sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:id="@+id/textView_QrSticker_serial"
                android:layout_below="@+id/space2"
                android:layout_alignParentStart="true"
                android:textSize="30sp" />
        </RelativeLayout>

    </LinearLayout>

Therefore I use the following code to perform this:

        public Bitmap viewToBitmap(View view) {
            Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            return bitmap;
        }

But If I run this code my bitmap only contains the ImageView (ImageView_qrcode). I checked it. If I inflate the Linearview the TextViews are shown correctly but the output bmp only contains the ImageView does somebody know why?

Upvotes: 2

Views: 2420

Answers (3)

Rod Kimble
Rod Kimble

Reputation: 1364

Thanks to @Gil Moshayof it works now!

After calling the getBitmapFromView method via post it works fine

                qrCodeSticker.setImageBitmap(((MainActivity) getActivity()).createQrCode(stringForQrCode));
                labelSticker.setText(qrDataList[1]);
                serialSticker.setText(qrDataList[2]);


                qrcodefinal.post(new Runnable() {
                    @Override
                    public void run() {
                        qrcodefinal.setImageBitmap(MainActivity.getBitmapFromView(qrStickerLayout));
                    }
                });

Upvotes: 1

ArtKorchagin
ArtKorchagin

Reputation: 4863

You can to try this 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;
}

There is solution with drawing cache:

public Bitmap getBitmapFromView(View view){
    view.setDrawingCacheEnabled(true);
    return view.getDrawingCache();
}

Upvotes: 6

rajahsekar
rajahsekar

Reputation: 914

Use setDrawingCacheEnabled(true);

View content = (LinearLayout)findViewById(R.id.linearLayout_QrCode);
    content.setDrawingCacheEnabled(true);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/" + yourimagename + ".png");
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
        content.invalidate();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
            content.setDrawingCacheEnabled(false);
    }

Upvotes: 2

Related Questions