老哥们,在安卓6.0以上,我想在代码中改变文件的读写权限,不成功,可能是什么原因呢?

diaostar 2019-02-12 04:27:30
我有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());
}
}
}



参考了很多老哥的方法,依旧不能获取到读写权限
...全文
301 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
usecf 2019-02-13
  • 打赏
  • 举报
回复
看看是不是有seliunx的问题
diaostar 2019-02-13
  • 打赏
  • 举报
回复
可以su,我电脑用adb指令可以修改,在代码中用adb指令就不行了
weixin_42573988 2019-02-12
  • 打赏
  • 举报
回复
无法su吧。
diaostar 2019-02-12
  • 打赏
  • 举报
回复
这种方法也不行 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; } } }

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧