-1

I want to take screenshot of an activity and show it in next activity on button click. I'm using following code but its not working.
Please let me know if anyone knows the answer:

choose.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    mainlayout.setDrawingCacheEnabled(true);
                    mainlayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    mainlayout.layout(0, 0, mainlayout.getMeasuredWidth(), mainlayout.getMeasuredHeight());
                    mainlayout.buildDrawingCache(true);
                    Bitmap b = Bitmap.createBitmap(mainlayout.getDrawingCache());
                    mainlayout.setDrawingCacheEnabled(false); // clear drawing cache
                    Intent i4=new Intent(Screen3.this,Screen4.class);
                    i4.putExtra("BitmapImage", b);
                    startActivity(i4);
                }//onclick
            });
Fabien
  • 12,486
  • 9
  • 44
  • 62
Zaa Ra
  • 67
  • 2
  • 10
  • 3
    "its not working" -> can you explain further what is not working ? – 2Dee Mar 02 '15 at 10:07
  • How do you get it in your `Screen4` activity? – Gorcyn Mar 02 '15 at 10:10
  • I want to take a screenshot of current activity and show it in next by intent. but code not works.. – Zaa Ra Mar 02 '15 at 10:11
  • see http://stackoverflow.com/questions/7762643/android-take-screen-shot-programatically – Skizo-ozᴉʞS ツ Mar 02 '15 at 10:11
  • im using this code in Screen4: public class Screen4 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screen4); ImageView cropimage=(ImageView)findViewById(R.id.cropimage); Intent intent = getIntent(); Bitmap b = (Bitmap) intent.getParcelableExtra("BitmapImage"); BitmapDrawable background = new BitmapDrawable(b); cropimage.setImageDrawable(background); } – Zaa Ra Mar 02 '15 at 10:14
  • Do you want to do the screenshoot when a button is clicked? – Skizo-ozᴉʞS ツ Mar 02 '15 at 10:14
  • @Skizo That is what the above code is saying – Gorcyn Mar 02 '15 at 10:22

1 Answers1

0
public static Bitmap captureScreen(View v){

        Bitmap bitmap = null;

        try {
            if(v!=null)
            {
                 int width = v.getWidth();
                 int height = v.getHeight();

                 Bitmap screenshot = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                 v.draw(new Canvas(screenshot));
                }
             } 
        catch (Exception e){

             Log.d("captureScreen", "Failed");
        }

     return bitmap;
     }

v is the what is being capturing (any layout)

  • can v be an imageview? or it must be any layout?? – Zaa Ra Mar 02 '15 at 10:18
  • and how i show this screenshot in next activity's imageview ? – Zaa Ra Mar 02 '15 at 10:25
  • Intent intent = new Intent(this,SecondActivity.class); intent.putExtra("bmp", bitmap); and in second activity Intent intent = getIntent(); Bitmap bmp = (Bitmap) intent.getParcelableExtra("bmp"); – Ajay Pandya Apr 25 '15 at 07:12