I have a root application that is supposed to capture the screen at some point during the execution. In order to accomplish this, I interact with the Android shell using the following code:
private static Process su = Runtime.getRuntime().exec("su");
private static DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
private static DataInputStream inputStream = new DataInputStream(su.getInputStream());
private void CaptureScreen() {
outputStream.writeBytes("/system/bin/screencap -p\n");
outputStream.flush();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//outputStream.writeBytes("echo test\n");
//outputStream.flush();
}
It works fine, even when I call it multiple times, but the moment I issue a dummy command that produces a shell output between CaptureScreen calls, BitmapFactory.decodeStream fails. Considering this, I have a few questions:
- I assume this happens because the data within the InputStream is no longer purely related to image data. Since the runtime is single instance (as referred to here), I again assume that other processes can also introduce their outputs within my InputStream on a live system. How can I make sure to only get the data I need from the InputStream?
- Why does CaptureScreen work correctly when called multiple times? How does BitmapFactory.decodeStream manage to get the last image from the InputStream? Does it "consume" relevant data when it succeeds? Does it search for the last image data from the InputStream? If so, why does it fail when there is irrelevant data before the image data within the InputStream?
I know I can get around this problem by writing the image to a file then read it from there, but I would like to avoid I/O operations in favor of performance.
0 comments:
Post a Comment