5

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.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
sravanthi
  • 183
  • 1
  • 3
  • 9
  • 1
    You can't do this in onCreate, since the UI is not drawn until afterwards. What are you trying to do? – Simon Dec 13 '12 at 07:40
  • As simon said, the code is right but you must put it in the right place. – faylon Dec 13 '12 at 07:41
  • i am doing this one outside of oncreate(),In view i am having zoomable bitmap value so i trying convert viw to bitmap and finally i am add that one to ImageView. – sravanthi Dec 13 '12 at 07:43
  • try searching http://stackoverflow.com/questions/7200535/how-to-convert-views-to-bitmap – Andy McSherry Dec 13 '12 at 07:46
  • my view values =packagename.classname@4053675 like this its coming ,so i am try to convert bitmap but its showing null.Help me – sravanthi Dec 13 '12 at 08:02
  • Invoke the code after all view are draw on the screen. – faylon Dec 13 '12 at 08:05

4 Answers4

6

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();
    }
}
alexgophermix
  • 4,189
  • 5
  • 32
  • 59
3

Use below code : just replace yourview value

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

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();

Reference

Community
  • 1
  • 1
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
  • my view values =packagename.classname@4053675 like this its coming ,so i am try to convert bitmap but its showing null.Help me – sravanthi Dec 13 '12 at 08:03
  • @sravanthi you have to override `onWindowsFocusChanged()` to get a non-null value :) – Si8 Feb 06 '14 at 01:58
1

Try This :

LinearLayout view1 = (LinearLayout) findViewById(R.id.linear1);
ImageView my_image = (ImageView) findViewById(R.id.my_imageView);

Bitmap b = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
view1.draw(c);

my_image.setImageBitmap(b); 

Hope it helps you.

Thanks.

Pratik Sharma
  • 13,307
  • 5
  • 27
  • 37
  • my view values =packagename.classname@4053675 like this its coming ,so i am try to convert bitmap but its showing null.Help me – sravanthi Dec 13 '12 at 08:04
  • You need to initialize your view before using and this will work out side of your onCreate method. Try to call this things on some button click event so it will allow to initialize your view before using. – Pratik Sharma Dec 13 '12 at 08:13
  • I have a layout which I am trying to get the background from. The layout's widthXheight is different from the bitmap's width and height :/ – Si8 Feb 06 '14 at 01:50
0

This could work

public static Bitmap getBitmapFromView(View view) {
   Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(returnedBitmap);
   view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
   view.draw(canvas);
   return returnedBitmap;
}
Set
  • 47,577
  • 22
  • 132
  • 150
Fergers
  • 473
  • 2
  • 7
  • 25