我有su权限,用adb可以修改成功,但是用代码就不行了,一直报Stream Close错误,哪位老哥实现过类似操作的?我用了三种方法,都不行 public void openCM(){
// String[] commands = new String[] { "/system/bin/sh", "-c", "chmod -R 777 /dev/video0" };
String[] commands = new String[] {"chmod -R 777 /dev/video0"};
Process process = null;
DataOutputStream dataOutputStream = null;
try {
process = Runtime.getRuntime().exec("su");
dataOutputStream = new DataOutputStream(process.getOutputStream());
int length = commands.length;
for (int i = 0; i < length; i++) {
dataOutputStream.writeBytes(commands[i] + "\n");
}
// dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();
process.waitFor();
} catch (Exception e) {
Log.d(TAG,"发生错误"+e.getMessage());
} finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
process.destroy();
} catch (Exception e) {
Log.d(TAG,e.getMessage());
}
}
}参考了很多老哥的方法,依旧不能获取到读写权限

解决方案 »

  1.   

    这种方法也不行public void open(){
    //获取权限
    File device = new File("/dev/video0");
    if (!device.canRead() || !device.canWrite()) {
    try {
    /* Missing read/write permission, trying to chmod the file */
    Process su;
    su = Runtime.getRuntime().exec("su");
    String cmd = "chmod 777 /dev/video0\nexit\n";
    su.getOutputStream().write(cmd.getBytes());
    if ((su.waitFor() != 0) || !device.canRead() || !device.canWrite()) {
    Log.d(TAG,"获取权限失败");
    return;
    }
    Log.d(TAG,"获取权限成功");
    } catch (Exception e) {
    e.printStackTrace();
    Log.d(TAG,"获取权限失败");
    return;
    }
    }
    }
      

  2.   

    可以su,我电脑用adb指令可以修改,在代码中用adb指令就不行了
      

  3.   

    看看是不是有seliunx的问题