在Android 9 - Pie上,由于安全策略的更改,您可能无法在运行时更改文件或目录的权限。在这种情况下,您可以使用以下方法解决此问题:
Process process = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
outputStream.writeBytes("chmod 777 /path/to/file\n");
outputStream.writeBytes("exit\n");
outputStream.flush();
process.waitFor();
这段代码使用Runtime.getRuntime().exec("su")
获取root权限并执行chmod
命令。您需要将/path/to/file
替换为您要更改权限的文件路径。
File file = new File("/path/to/file");
file.setWritable(true, false);
file.setReadable(true, false);
file.setExecutable(true, false);
这种方法使用File类的setWritable、setReadable和setExecutable方法来更改文件的权限。您需要将/path/to/file
替换为您要更改权限的文件路径。
请注意,第一种方法需要您的设备已获取root权限,而第二种方法不需要root权限。根据您的需求和设备状态,选择适合的方法。