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
Asked
Active
Viewed 77 times
0
-
1Which OS are you talking about? – Michael Sep 11 '14 at 13:58
-
Sorry - kitkat and jelly bean – carpman Sep 11 '14 at 13:59
-
You can't take a screenshot of the entire screen. Only of your app's content (and like Ingress, you'll get your whole layout. Even off screen stuff). http://stackoverflow.com/questions/11430418/capture-screen-shot-on-android-using-java-code – Erik Nedwidek Sep 11 '14 at 14:07
2 Answers
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