Java – Android screenshot2 The tool NullPointerException on Linux

Android screenshot2 The tool NullPointerException on Linux… here is a solution to the problem.

Android screenshot2 The tool NullPointerException on Linux

I’m using:

Java version: 7

SDK tools, revision 24.4.1 (October 2015).

Taking a screenshot using the tool screenshot2 throws a NullPointerException on Ubuntu Server 14.04 and Kali Linux 2.0. It works with Mac OS X 10.11.

Command:

$ screenshot2 -e image.png

(An error occurs with or without the -s parameter).

Error:

Exception in thread "main" java.lang.NullPointerException
at com.android.ddmlib.MonitorThread.wakeup(MonitorThread.java:580)
at com.android.ddmlib.MonitorThread.quit(MonitorThread.java:588)
at com.android.ddmlib.AndroidDebugBridge.terminate(AndroidDebugBridge.java:235)
at com.android.screenshot.Screenshot.main(Screenshot.java:198)

Is there a fix or does anyone know what the reason is?

Solution

I found a solution to avoid using the screenshot2 tool.

Dependencies for Linux:

sudo apt-get install zlib1g-dev libjpeg-dev

Maybe it works even without being on your machine, but I don’t think so.

Then you need python package pillow version 2.9.0

Mac and Linux dependencies:

sudo pip install pillow==2.9.0

I made this very basic script to create screenshot images.

File screenshot .py

from StringIO import StringIO
from PIL import Image
import sys

binary = sys.stdin.read().replace('\r\n', '\n')
stream = StringIO(binary)
image = Image.open(stream)
image.save(sys.argv[1], 'PNG')

Now you can simply take a screenshot from your device using the command line like I tried above.

Example:

adb shell screencap -p | python screenshot.py image.png

Or use the -s parameter for a specific device

adb shell -s emulator-5558 screencap -p | python screenshot.py image.png

Related Problems and Solutions