问题描述
我想从Android上的运行命令捕获stdout输出。下面是使用的代码段。
Process screenRecordProcess = null;
private void startScreenRecord(){
new Thread(){
@Override
public void run() {
Log.d(TAG,"startScreenRecord");
int total = 0;
try{
String cmd = "screenrecord --output-format=h264 -";
//screenRecordProcess = new ProcessBuilder("screenrecord","--output-format=h264","-").start();
screenRecordProcess = Runtime.getRuntime().exec(cmd);
Log.d(TAG,"executed");
InputStream is = screenRecordProcess.getInputStream();
int read;
byte[] buffer = new byte[4096];
while ((read = is.read(buffer)) > 0) {
Log.d(TAG,"read: "+read);
total += read;
}
is.close();
int exitValue = screenRecordProcess.waitFor();
Log.d(TAG,"waited for end: "+exitValue);
} catch (Exception e) {
Log.e(TAG,"run screenrecord interrupted",e);
}
screenRecordProcess = null;
Log.d(TAG,"run screenrecord ended total: "+total);
}
}.start();
}
private void stopScreenRecord(){
Log.d(TAG,"stopScreenRecord");
if(screenRecordProcess != null){
screenRecordProcess.destroy();
}
}
尽管我可以从InputStream
中获取Process
,但是我无法从中读取任何字节。(total
最后仍然为0)是否是权限问题?但是,我可以在adb shell
上运行完全相同的命令,它将在shell上输出“不可读”(视频)字节。
我在哪里做错了?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)