6

我正在开发一个可以将应用程序的数据恢复到 /data/data/{packageName} 的应用程序。恢复文件后,我将权限设置为 rw- r-- r--。我是这样设置的:

public int chmod(File path, int mode) throws Exception {
    Class fileUtils = Class.forName("android.os.FileUtils");
    Method setPermissions = fileUtils.getMethod("setPermissions",
            String.class, int.class, int.class, int.class);
    return (Integer) setPermissions.invoke(null, path.getAbsolutePath(),
            mode, -1, -1);
}

并打电话chmod(file, 644);

但是当我在文件资源管理器中检查这些文件的权限时,它会显示“--- rwx rx”。

那么如何将权限设置为 rw- r-- r--?

4

3 回答 3

6
Process process = null;
DataOutputStream dataOutputStream = null;

try {
    process = Runtime.getRuntime().exec("su");
    dataOutputStream = new DataOutputStream(process.getOutputStream());
    dataOutputStream.writeBytes("chmod 644 FilePath\n");
    dataOutputStream.writeBytes("exit\n");
    dataOutputStream.flush();
    process.waitFor();
} catch (Exception e) {
    return false;
} finally {
    try {
        if (dataOutputStream != null) {
            dataOutputStream.close();
        }
        process.destroy();
    } catch (Exception e) {
    }
}
于 2013-01-28T10:02:29.133 回答
4

值不对,正确的是420(十进制的420就是八进制的644)。或者,您可以添加前导0以使其成为 java 八进制文字。IE

chmod(destinationFile, 0644)
于 2013-01-28T11:25:33.003 回答
3

您应该能够使用以下命令对文件设置这些权限(rw- r-- r--):

path.setReadOnly(true); //Sets all permissions for every owner back to read-only
path.setWritable(true); //Sets the owner's permissions to writeable

path你的File对象在哪里。

您不需要使用带有反射的 FileUtils 来设置文件的权限。您可以只使用File类上的辅助方法。使用 File,您还可以调用:setReadable()setWritable()setExecutable()

于 2016-09-07T19:28:13.173 回答