Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert View to Bitmap in android?

I'm using following line to convert view to bitmap in android.

view.setDrawingCacheEnabled(true);
Bitmap b= view.getDrawingCache(); 

I am having value in view when I am converting to Bitmap, its showing bitmap value null.

like image 298
sravanthi Avatar asked Dec 13 '12 07:12

sravanthi


2 Answers

Activity Class In this Class I Convert the Layout(View) into Image.

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ImageActivity extends Activity {

    LinearLayout layout=null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convert);
        layout=(LinearLayout)findViewById(R.id.layout);
        ((ImageView)findViewById(R.id.ImageView01)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Bitmap map=ConvertToBitmap(layout);
                Log.v("BitmapObject", map.toString());
            }
        });       
    }

    protected Bitmap ConvertToBitmap(LinearLayout layout) {
        Bitmap map;
        layout.setDrawingCacheEnabled(true);
        layout.buildDrawingCache();
        return map=layout.getDrawingCache();
    }
}
like image 137
Siva Pothana Avatar answered Nov 14 '22 23:11

Siva Pothana


Use below code : just replace yourview value

YourView view = (YourView )findViewById(R.id.yourViewId);

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();

Reference

like image 26
swiftBoy Avatar answered Nov 14 '22 21:11

swiftBoy