我有一個根應用程序應該在執行期間的某個點捕獲屏幕。爲了做到這一點,我與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操作而改用性能。