Search code examples
javaandroidscreenshotframebuffer

Android take screenshot from code, with root access


I need to take a screenshot from a service running in the background. The screenshot can be of the app in the foreground, even if its not my app, which is why I can't use DrawingCache. I've searched around and found this

process = Runtime.getRuntime().exec("su -c cat /dev/graphics/fb0"); InputStream is = process.getInputStream();

But I don't know what to do with the stream. How do I get it into a bitmap? I don't need to save it to file I just need to keep it in a variable. This is for rooted phones.

Any help is much appreciated.


Solution

  • Well, seems I'm on my own with this one. So far this is what I've found, for those who have a similar problem. Note that this varies by version, and I am running 2.3 Gingerbread.

    I found 2 files in /system/bin - screencap and screenshot.

    screencap returns a bitmap in a format I don't know how to decode, while screenshot makes a regular bmp file called tmpshot.bmp at the root of the sdcard (Neither accept any kind of command parameters. They don't seem to accept -h,-help, --help etc either).

    tmpshot.bmp seems a viable solution for now but I was looking to avoid writing and reading from sdcard as this is quite the bottleneck. I will keep searching, but for those who need a solution now, here you are.

    EDIT: Upon further trials I found the screencap binary from another rom (MIUI to be specific) actually offers a few more options, where (miui)screencap -t png /sdcard/screen.png yields a valid png screenshot.