Skip to content Skip to sidebar Skip to footer

How To Take Screenshot Of Whole Activity Page Programmatically?

How can I take screenshot of whole activity on Android, even if content is not visible? Ex. take screenshot of full chat and then generate image file? I want screenshot invisible

Solution 1:

One way is to extract the bitmap of the current screen and save it. Try this:

publicvoidtakeScreenshot(){
    Bitmap bitmap = getScreenBitmap(); // Get the bitmap
    saveTheBitmap(bitmap);               // Save it to the external storage device.
}

public Bitmap getScreenBitmap() {
   View v= findViewById(android.R.id.content).getRootView();
   v.setDrawingCacheEnabled(true); 
   v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
   v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

   v.buildDrawingCache(true);
   Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
   v.setDrawingCacheEnabled(false); // clear drawing cachereturn b;
}

Android take screen shot programmatically

Solution 2:

This is one way to take "screenshot" of a view. I haven't tested it so tell if it works

//View v is the root view/parent of your layout, for example LinearLayout or RelativeLayout (layout where you have placed content of which you want to take screenshot)private Bitmap getBitmapFromView(View v) {
    Bitmapbitmap= Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvascanvas=newCanvas (bitmap);
    v.draw(canvas);
    // Returns screenshotreturn bitmap;
}

Solution 3:

public class MainActivity extends Activity {

ImageViewimageView=null;
ScrollViewsv=null;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sv = (ScrollView) findViewById(R.id.ssscrollView);
    imageView = (ImageView) findViewById(R.id.imgView);
    ButtonmyBtn= (Button) findViewById(R.id.btn);
    myBtn.setOnClickListener(newView.OnClickListener() {

        @SuppressWarnings("deprecation")@OverridepublicvoidonClick(View v) {

            Viewv1= sv.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmapbm= getBitmapFromView(v1);
            @SuppressWarnings("deprecation")BitmapDrawablebitmapDrawable=newBitmapDrawable(bm);
            ImageViewview2= (ImageView) findViewById(R.id.imgView);
            view2.setBackgroundDrawable(bitmapDrawable);
        }
    });

}

private Bitmap getBitmapFromView(View v) {
    Bitmapbitmap= Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvascanvas=newCanvas(bitmap);
    v.draw(canvas);
    return bitmap;
}

}

Post a Comment for "How To Take Screenshot Of Whole Activity Page Programmatically?"