13

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?

enter image description here

I want screenshot invisible area too.

Thanks

Giorgi Asaturyan
  • 383
  • 5
  • 15
  • I tried android ASL library, but demo don`t works properly. https://code.google.com/p/android-screenshot-library/ – Giorgi Asaturyan Dec 25 '14 at 20:26
  • I'm not even sure it's possible or not technically – Giorgi Asaturyan Dec 25 '14 at 20:28
  • It's possible for sure, given that your Activity is visible. – Phantômaxx Dec 25 '14 at 20:29
  • @DerGolem: Not necessarily. In fact, in the general case, it is impractical enough to be "impossible" for reasonable levels of effort. Anything that uses an adapter pattern (`ListView`, `RecyclerView`, `ViewPager`, etc.) will not work with a "classic" screenshot approach, because not all of the adapter items are presently rendered in `Views`. Instead, you would have to basically stitch together your own screenshot by manually rendering each adapter item individually and pretending like they were all on the screen. You might not have heap space for that. – CommonsWare Dec 25 '14 at 20:49
  • 1
    Why a screenshot? For a "full chat", saving the output as an image would not be my choice. I would go with HTML, or perhaps PDF, so that the text is still searchable, copy-able, etc. – CommonsWare Dec 25 '14 at 20:50
  • it is possible to screenshot images while scrolling? :) WHat do you thinks - this is good idea? – Giorgi Asaturyan Dec 25 '14 at 21:06
  • @CommonsWare Yes, you can't screenshot the **whole** ListView. In facts, a screenshot, as the name suggests, is a **screen** shot: **What you see** is what you get. To save a ListView contents, I'd go for a **CSV** file (easy to generate without 3rd party libraries and compatible with all OS and spreadsheets). – Phantômaxx Dec 26 '14 at 09:58
  • https://stackoverflow.com/a/54779181/7074112 Check this answer for a possible solution – Peter Feb 20 '19 at 05:07

3 Answers3

8

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

public void takeScreenshot(){
    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 cache
   return b;
}

Android take screen shot programmatically

Community
  • 1
  • 1
thepace
  • 2,131
  • 12
  • 20
1

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) {
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas (bitmap);
    v.draw(canvas);
    // Returns screenshot
    return bitmap;
}
user1888162
  • 1,735
  • 21
  • 27
0

public class MainActivity extends Activity {

ImageView imageView = null;
ScrollView sv = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {

            View v1 = sv.getRootView();
            v1.setDrawingCacheEnabled(true);
            Bitmap bm = getBitmapFromView(v1);
            @SuppressWarnings("deprecation")
            BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
            ImageView view2 = (ImageView) findViewById(R.id.imgView);
            view2.setBackgroundDrawable(bitmapDrawable);
        }
    });

}

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

}

Giorgi Asaturyan
  • 383
  • 5
  • 15