首先确保您需要的shell命令在
Android中实际可用.假设您可以使用>重定向输出,我遇到了问题.
这个方法也适用于我相信v2.2的非root手机,但你应该检查API参考以确定.
try {
Process chmod = Runtime.getRuntime().exec("/system/bin/chmod 777 " +fileName);
BufferedReader reader = new BufferedReader(
new InputStreamReader(nfiq.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
chmod.waitFor();
outputString = output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
虽然它可能不是100%必要的,但是让进程等待exec完成process.waitFor()是个好主意,因为你说你关心输出.