我有一个根应用程序应该在执行期间的某个点捕获屏幕。为了做到这一点,我与Android外壳交互使用下面的代码:确保从InputStream读取正确的数据
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();
}
它正常工作,甚至当我把它称为多次,但此刻我发出产生之间的外壳输出的虚拟指令CaptureScreen调用,BitmapFactory.decodeStream失败。考虑到这一点,我有几个问题:
- 我想这是因为InputStream中内的数据是没有 不再是单纯的与图像数据有关。由于运行时间是单个 实例(如here),我再次假设其他 进程也可以在我的InputStream中的 实时系统中引入它们的输出。我如何确保只从 InputStream获取我需要的数据?
- 为什么CaptureScreen在被多次调用时工作正常?如何 BitmapFactory.decodeStream管理从 InputStream获取最后一张图片?成功后是否“消耗”相关数据? 它是否从InputStream搜索最后的图像数据?如果是这样,为什么当 InputStream中的图像数据之前存在不相关的数据时,它会失败 ?
我知道我可以通过将图像写入文件然后从那里读取来解决此问题,但是我希望避免I/O操作而改用性能。