How to get file resolution mode programmatically in Java

I know that you can change the file permissions mode using:

Runtime.getRuntime().exec( "chmod 777 myfile" ); .

In this example, the permission bits are set to 777 . Is it possible to programmatically set the resolution bits to 777 using Java? Can this be done for each file?

+4
source share
4 answers

Using chmod on Android

Java does not have built-in support for platform-dependent operations such as chmod. However, Android provides utilities for some of these operations through android.os.FileUtils. The FileUtils class is not part of the public SDK and is therefore not supported. Therefore, 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); ... 

Link: http://www.damonkohler.com/2010/05/using-chmod-in-android.html?showComment=1341900716400#c4186506545056003185

+9
source

Android makes it difficult to interact with other applications and their data, with the exception of Intents. Skills will not work for permissions because you are dependent on the application receiving the intent to execute / provide what you want; they were probably not intended to not allow anyone the permissions of their files. There are ways around this, but only when the applications are designed to run in the same JVM. Therefore, each application can only change it. for more details on file resolution see http://docs.oracle.com/javase/1.4.2/docs/guide/security/permissions.html

+1
source

As noted earlier, android.os.FileUtils has changed and the solution sent by Ashraf no longer works. The following method should work on all versions of Android (although it uses reflection, and if the manufacturer has 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 have a file to make any changes.

+1
source

Here is a solution using Apache Commons.IO FileUtils and the corresponding File methods.

 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 calls to set{Read,Writ,Execut}able to implement other modes. (I would love to update this answer if someone posted the appropriate code for this.)

0
source

All Articles