创建一个具有多个函数的类,我遇到了其中一个函数的问题,这些函数将使用ProcessBuilder发送命令并将其输出作为字符串,充当shell命令。当我使用它时,它会在InputStreamReader中捕获带有错误文件号的IOException,并返回“ERROR !!!” ,这是我的代码,如果你需要了解这个评论的任何内容......我正在使用命令“ls”btw,“ls / sdcard /”......
public String builder(String str) {
startTimer(); // A method that gets the current time as int
String[] array = str.split(" " , 2);
String cmd = array[0] + " " + array[1];
System.out.println("\nexecuting: " + cmd + "----");
String AllText = "";
try {
String anyString;
//Process process = new ProcessBuilder("sh","-c","\"",cmd,"\"").start();
Process process = new ProcessBuilder(array).start();
BufferedReader OUT = new BufferedReader(new InputStreamReader(process.getInputStream())); // Here is My error
BufferedReader ERR = new BufferedReader(new InputStreamReader(process.getErrorStream()));
try {
for (int x = endTimer()/* a method which subtracts the startTimer() from current time to get the time passed */ ; endTimer()<= 5000 ; x++){
process.waitFor();
}process.destroy();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
while ((anyString = ERR.readLine()) != null) {
AllText = AllText + "\n" + anyString;
}
while ((anyString = OUT.readLine()) != null) {
AllText = AllText + "\n" + anyString;
while ((anyString = ERR.readLine()) != null) {
AllText = AllText + "\n" + anyString;
}
}
return AllText;
} catch (Exception ex) {
ex.printStackTrace();
return "ERROR!!!";
}
按下按钮时,这是堆栈跟踪:
12-03 18:55:36.475: W/System.err(17973): java.io.IOException: read failed: EBADF (Bad file number)
12-03 18:55:36.507: W/System.err(17973): at libcore.io.IoBridge.read(IoBridge.java:442)
12-03 18:55:36.507: W/System.err(17973): at java.io.FileInputStream.read(FileInputStream.java:179)
12-03 18:55:36.507: W/System.err(17973): at java.io.InputStreamReader.read(InputStreamReader.java:244)
12-03 18:55:36.507: W/System.err(17973): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
12-03 18:55:36.507: W/System.err(17973): at java.io.BufferedReader.readLine(BufferedReader.java:354)
12-03 18:55:36.507: W/System.err(17973): at com.example.activity.over.other.Shells.builder(Shells.java:542)
12-03 18:55:36.507: W/System.err(17973): at com.example.activity.over.other.MainActivity$2.onClick(MainActivity.java:91)
12-03 18:55:36.514: W/System.err(17973): at android.view.View.performClick(View.java:4202)
12-03 18:55:36.514: W/System.err(17973): at android.view.View$PerformClick.run(View.java:17340)
12-03 18:55:36.514: W/System.err(17973): at android.os.Handler.handleCallback(Handler.java:725)
12-03 18:55:36.514: W/System.err(17973): at android.os.Handler.dispatchMessage(Handler.java:92)
12-03 18:55:36.514: W/System.err(17973): at android.os.Looper.loop(Looper.java:137)
12-03 18:55:36.514: W/System.err(17973): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-03 18:55:36.514: W/System.err(17973): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 18:55:36.522: W/System.err(17973): at java.lang.reflect.Method.invoke(Method.java:511)
12-03 18:55:36.522: W/System.err(17973): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-03 18:55:36.522: W/System.err(17973): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-03 18:55:36.522: W/System.err(17973): at dalvik.system.NativeStart.main(Native Method)
12-03 18:55:36.522: W/System.err(17973): Caused by: libcore.io.ErrnoException: read failed: EBADF (Bad file number)
12-03 18:55:36.530: W/System.err(17973): at libcore.io.Posix.readBytes(Native Method)
12-03 18:55:36.530: W/System.err(17973): at libcore.io.Posix.read(Posix.java:123)
12-03 18:55:36.530: W/System.err(17973): at libcore.io.BlockGuardOs.read(BlockGuardOs.java:149)
12-03 18:55:36.530: W/System.err(17973): at libcore.io.IoBridge.read(IoBridge.java:432)
12-03 18:55:36.530: W/System.err(17973): ... 17 more
答案 0 :(得分:-1)
检查清单文件中是否遗漏了这个。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />