【发布时间】:2013-01-11 14:47:35
【问题描述】:
我正在开发一个应用程序,可以将应用程序的数据恢复到 /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 r-x”。
那么如何设置权限为 rw- r-- r--?
【问题讨论】:
-
我认为您需要超级用户访问权限才能这样做
-
是的。我已经获得了 SU 访问权限。字符串 cmd = "cp -r " + sourceFile.getAbsolutePath() + "/* " + destinationFile.getAbsolutePath() + "/"; Runtime.getRuntime().exec(new String[] { "su", cmd }); chmod(destinationFile, 644);
-
@Rajkiran 我也想更改文件的权限,在搜索时我遇到了你的问题。我尝试了相同的代码,但在
android.os包中找不到FileUtils类。你是怎么用的??请回复here。 -
您真正搜索的是什么? android.os 包有一个同名的文件。源代码:grepcode.com/file/repository.grepcode.com/java/ext/…