Rajkiran Rajkiran - 1 year ago 143
Android Question

Android how to set rw- r-- r-- permissions programmatically

I am developing an application that can restore apps' data back to /data/data/{packageName}. After restoring the files, I am setting the permissions to rw- r-- r--. I set it in this way:

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

and calling
chmod(file, 644);

But when I check these files' permissions in file explorer it shows me "--- rwx r-x".

So how can I set the permissions to rw- r-- r--?

Answer Source
Process process = null;
DataOutputStream dataOutputStream = null;

try {
    process = Runtime.getRuntime().exec("su");
    dataOutputStream = new DataOutputStream(process.getOutputStream());
    dataOutputStream.writeBytes("chmod 644 FilePath\n");
} catch (Exception e) {
    return false;
} finally {
    try {
        if (dataOutputStream != null) {
    } catch (Exception e) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download