微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何以编程方式设置rw- r-权限?

我正在开发一个可以将应用程序的数据恢复到/ 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);
    return (Integer) setPermissions.invoke(null,path.getAbsolutePath(),mode,-1,-1);
}

调用chmod(文件,644);

但是当我在文件浏览器中检查这些文件的权限时,它会显示“— rwx r-x”.

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

解决方法

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) {
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐