1

From my app, I am trying to take a full device screenshot equivalent of the user holding their device up-volume button and power button.

I tried this approach:

rootView = view.getRootView();
Bitmap bitmap = Bitmap.createBitmap(rootView.getWidth(), rootView.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
rootView.draw(canvas);

However, this is only drawing my application, and poorly at that, because if I have a menu open, or keyboard open when I take my screenshot, those areas are blacked out. :(

Someone recommended this to me:

  • if you're calling getRootView() on a view, you should end up with a screenshot of the entire app
  • taking a screenshot of the entire phone UI requires root or a running system service
  • there may be a way to make this work via the draw-overlay permission (which we now have) and creating a temporary transparent activity, but that's a fair amount of work

I can't root the phone, so I was wondering if someone could guide me to an example of code which does the "draw overlay - temp transparent activity" method to get a full device screenshot. I'm new to android, so I was having trouble finding it.

Wandering Fool
  • 2,064
  • 3
  • 15
  • 44
Noitidart
  • 30,310
  • 26
  • 103
  • 267
  • 2
    "From my app I am trying to take a full device screenshot, equivalent of user holding their device up volume button and power button" -- fortunately, this is not possible, prior to Android 5.0, for obvious privacy and security reasons. That's why it historically required rooted devices. On Android 5.0+, there is a supported means of doing this, but it requires getting on-demand user acceptance via a dialog box. – CommonsWare Aug 09 '15 at 17:34
  • Aw dang thanks @CommonsWare may you please show me the Android 5+ method, user acceptance is no problem, my app is a screenshot app. :) I'll have to go with whatever I can for now :( Although ideally i would have loved to support <5 too :( – Noitidart Aug 09 '15 at 17:35
  • `this is only drawing my application` - by design. – Phantômaxx Aug 09 '15 at 17:35
  • 2
    I haven't played with the APIs yet, but there is an overview in [the Android 5.0 release notes](http://developer.android.com/about/versions/android-5.0.html#ScreenCapture). – CommonsWare Aug 09 '15 at 17:37
  • Thanks @CommonsWare I'll look into that! Ill also be on look out for any hacky solutions for non-rooted phones <5.0 and let you know if I come up with anything. :) – Noitidart Aug 09 '15 at 17:41
  • Hey @CommonsWare what about with API16+ with `android.permission.SYSTEM_ALERT_WINDOW` if I write on top of the device root view, make it transparent, and width height `MATCH_PARENT` so it takes the entire screen, and then get a shot of this/"your" view. I badly need to get a screenshot method so it matches my Windows, OSX, iOS, and Linux/Unix versions of my app, otherwise users will be unhappy, its no security thing as the user is intentionally taking a screenshot. :( Like proof of concepts here: http://stackoverflow.com/questions/4481226/creating-a-system-overlay-window-always-on-top – Noitidart Aug 10 '15 at 02:01
  • 1
    "what about..." -- you should get a transparent image. "I badly need to get a screenshot method" -- no, you need to set a `minSdkVersion` of 21, then use the officially supported stuff on Android 5.0+. "its no security thing" -- yes, it is. Just because *you* may be a saint does not mean that the world's malware authors magically become saints themselves. – CommonsWare Aug 10 '15 at 10:56
  • Thanks @CommonsWare ive already started writing the 5+ stuff that without question is what im using when its available but i need support for 4.* the stupid service providers never update :( can you please explain the transparent image thingy? across the nation they only recently updated us to 4.* :( – Noitidart Aug 10 '15 at 19:00
  • Also @CommonsWare I promise i am a very honest/saint. all is open source work, this is for my firefox addon NativeShot which is totally open source here: https://addons.mozilla.org/en-US/firefox/addon/nativeshot/ and https://github.com/Noitidart/NativeShot and my beta want on android here is my "view shot" code: https://github.com/Noitidart/_scratchpad/blob/47ab2775aa1dc0c7de240cdfe47d32b635467112/_jni-Screenshot.js and here is my trying window overlay: https://github.com/Noitidart/_scratchpad/blob/master/_jni-WindowManagerOverlay.js i get $0 for this. i dont get any support for mozilla even – Noitidart Aug 10 '15 at 21:23

0 Answers0