Stack Overflow archive
1 score

How to get File Permission Mode programmatically in Java

score
1
question views
9.1K
license
CC BY-SA 3.0

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).

bash
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.

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.