0

I have seen many different replies to this so can anyone give a positive reply please? Does a device running a recent OS need to be rooted to take screenshots from code? I am trying to do this and I just get a black picture (of the same dimensions as my screen). Many thanks

Michael
  • 57,169
  • 9
  • 80
  • 125
carpman
  • 75
  • 8

2 Answers2

2

If you want to take screenshots of other apps apart from your own.

Yes the device need to be rooted.

I have also searched a bit about it and never found a solution.

Even after rooting there is no definite solution that works on all models.

user2800040
  • 143
  • 2
  • 13
0

To take a screenshot of other apps you need to have root. In order to take the screenshot you use the framebuffer.

To take a screenshot of your application, you can take a screenshot of the activity with the code from here: https://stackoverflow.com/a/23927196/3029105

This will get a bitmap of the activity.

Code:

public class MainAvtivity extends Activity
{
    ImageView imgView;
    Button btnScreenshot;
    View rootView;
    Bitmap bmap;

    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        setContentView(R.layout.main)

        imgView = (ImageView)findViewById(R.id.imageView);
        btnScreenshot = (Button)findViewById(R.id.btnScreenshot);
        rootView = findViewById(android.R.id.content).getRootView();

        btnScreenshot.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                imgView.setImageBitmap(null);
                bmap = takeScreenshot();
                setImgView(bmap);
            }
    }
    public Bitmap takeScreenshot()
    {
        rootView.setDrawingCacheEnabled(true);
        rootView.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(rootView.getDrawingCache());
        rootView.setDrawingCacheEnabled(false);
        return b;
    }
    public void setImgView(Bitmap bitmap)
    {
        imgView.setImageBitmap(bitmap);
    }
}
Community
  • 1
  • 1
CodeMonkey
  • 1,136
  • 16
  • 31
  • Thank you for the reply. I have tried your code and many other peoples but I always get a black picture. I assume I am not finding the correct view because if I use canvas and manually draw to that canvas it works fine and I can see the drawing in the screenshot. Perhaps I am not calling the code from the correct place? I am doing so in my renderer after all rendering is complete. – carpman Sep 11 '14 at 14:35