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
  • 393
  • 1
  • 5
  • 16
  • 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

9

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,221
  • 1
  • 13
  • 21
  • thanks, yes it`s works, but i need screenshot of full activity - not only visible. :) – Giorgi Asaturyan Dec 25 '14 at 20:42
  • Full Activity as in? Could you share a screen shot of the phone and the screenshot content required? Full activity as in the current and the others that have been created in the background? – thepace Dec 25 '14 at 20:45
  • 1
    I have edited my question , please see image above and you will understand what i need :) thx – Giorgi Asaturyan Dec 25 '14 at 21:10
  • Please try the above code. Hope it helps. – thepace Dec 25 '14 at 21:27
  • Please ensure you accept the correct answer that works for you so that the queston does not appeared in the unanswered section. – thepace Dec 25 '14 at 22:09
  • whta is content here? view or viewgroup?? – abh22ishek Nov 04 '15 at 09:57
  • v.getDrawingCache() gives a null bitmap. – user2331595 Apr 26 '17 at 20:03
  • https://github.com/peter1492/LongScreenshot have a look if its useful – Peter Feb 15 '19 at 11:14
  • This code return null exception sometimes, I edited some part of code like this. Other part of code is same. use this---------------Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas (b); v.draw(canvas); v.setDrawingCacheEnabled(false);----------------------------instead of--------------------------------Bitmap b = Bitmap.createBitmap(v.getDrawingCache()); – Kanan Feb 03 '23 at 06:51
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
  • Thanks for your answer, yes it`s works but i need screenshot of full activity - not only visible.. you got me? :) – Giorgi Asaturyan Dec 25 '14 at 20:38
  • @Giorgi Asaturyan Set scrollview as a parent view. That way you can take screenshot even if the content is not visible. Or if your content is inside a ListView the you could automatically scroll down or up and take multiple screenshots of it then combine them all together to one bitmap. It isn't so hard. – user1888162 Dec 25 '14 at 21:13
  • I`ve tried right now , but still it doesn`t works for me Can you share some working code ? thx – Giorgi Asaturyan Dec 25 '14 at 21:21
  • Post your code and i see what i can do. – user1888162 Dec 25 '14 at 21:24
  • https://github.com/peter1492/LongScreenshot have a look if its useful – Peter Feb 15 '19 at 11:14
1

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
  • 393
  • 1
  • 5
  • 16