Reputation: 2271
I know it is possible to change the permission mode of a file using:
Runtime.getRuntime().exec( "chmod 777 myfile" );
.
This example sets the permission bits to 777
. Is it possible to set the permission bits to 777
programmatically using Java? Can this be done to every file?
Upvotes: 4
Views: 9042
Reputation: 38131
As commented previously, android.os.FileUtils
has changed and the solution posted by Ashraf no longer works. The following method should work on all versions of Android (although it does use reflection and if a manufacturer made major changes this may not work).
public static void chmod(String path, int mode) throws Exception {
Class<?> libcore = Class.forName("libcore.io.Libcore");
Field field = libcore.getDeclaredField("os");
if (!field.isAccessible()) {
field.setAccessible(true);
}
Object os = field.get(field);
Method chmod = os.getClass().getMethod("chmod", String.class, int.class);
chmod.invoke(os, path, mode);
}
Obviously you will need to own the file to make any permission changes.
Upvotes: 1
Reputation: 1748
Here is a solution using Apache Commons.IO FileUtils, and the appropriate methods on File objects.
for (File f : FileUtils.listFilesAndDirs(new File('/some/path'), TrueFileFilter.TRUE, TrueFileFilter.TRUE)) {
if (!f.setReadable(true, false)) {
throw new IOException(String.format("Failed to setReadable for all on %s", f));
}
if (!f.setWritable(true, false)) {
throw new IOException(String.format("Failed to setWritable for all on %s", f));
}
if (!f.setExecutable(true, false)) {
throw new IOException(String.format("Failed to setExecutable for all on %s", f));
}
}
This is the equivalent of chmod -R 0777 /some/path
. Adjust the set{Read,Writ,Execut}able
calls to implement other modes. (I'll happily update this answer if someone posts appropriate code to do this.)
Upvotes: 0
Reputation: 3192
Using chmod in Android
Java doesn't have native support for platform dependent operations like chmod. However, Android provides utilities for some of these operations via android.os.FileUtils. The FileUtils class is not part of the public SDK and is therefore not supported. So, use this at your own risk:
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("/foo/bar/baz", 0755);
...
Upvotes: 9
Reputation: 4481
Android make it difficult to interact with other applications and their data except through Intents. Intents won't work for permissions because you're dependent on the application receiving the Intent to do/provide what you want; they probably weren't designed to tell anybody their files' permissions. There are ways around it, but only when the applications are desgned to operate in the same JVM. So each application can only change it's file. for more detail in file permission see http://docs.oracle.com/javase/1.4.2/docs/guide/security/permissions.html
Upvotes: 1