我想在adb shell执行的设备上做截图。在Android 5.0上,一些命令是公共的,意味着不需要root权限。
当我尝试执行该命令时,我没有获得权限失败,而是我得到了这个例外:
java.io.IOException: Error running exec(). Command: [/system/bin/adb, shell, screencap, -p, /sdcard/screencap.png] Working Directory: /system/bin/adb Environment: null
at java.lang.ProcessManager.exec(ProcessManager.java:211)
at java.lang.Runtime.exec(Runtime.java:173)
at java.lang.Runtime.exec(Runtime.java:246)
at MyActivity.executeCommand(MyActivity.java:181)
at MyActivity$2.run(MyActivity.java:170)
at java.lang.Thread.run(Thread.java:818)
System.err﹕ Caused by: java.io.IOException: Not a directory
at java.lang.ProcessManager.exec(Native Method)
at java.lang.ProcessManager.exec(ProcessManager.java:209)
... 5 more
为什么我的环境为空?
这是我的代码:
public static boolean executeCommand(String command, String path) {
System.out.println("executing command: " + command);
Process p;
try {
p = Runtime.getRuntime().exec(command, null, new File(path));
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder output = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
if (output.length() > 0) {
System.out.println("Result: " + output.toString());
}
if (p.getErrorStream().available() > 0) {
reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
output = new StringBuilder("");
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
if (output.length() > 0) {
System.out.println("Result: " + output.toString());
}
return false;
}
PrintStream printStream = new PrintStream(p.getOutputStream());
printStream.println();
printStream.flush();
}
catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
这是执行:
Thread t = new Thread(new Runnable() {
@Override
public void run() {
String screencapCmd = "/system/bin/adb shell screencap -p /sdcard/" + "screencap.png";
boolean screenCapTaken = executeCommand(screencapCmd, "/system/bin/adb");
}
});
t.start();