How to change the file's permission in android using chmod

8.6k Views Asked by At

I'm using chmod command to change file's permission but it's doesn't work in my code and my device is rooted too:

String MyFilePath = "/data/data/MyPkgName/database/com.pocketchange.android.http.AsyncHttpRequestService.db";
CommandCapture command = new CommandCapture(0, "chmod  777 " + "\""+ MyFilePath+ "\"");
RootTools.getShell(true).add(command).waitForFinish();

And when it's execute, the command output is :

Unable to chmod /data/data/MyPkgName/databases/com.pocketchange.android.http.AsyncHttpRequestService.db: No such file or directory

but the file is exist, and when I use this code to change the permission of another file in the database directory its success, so what can I do now.

2

There are 2 best solutions below

0
On

Use the chmod command in the ADB shell...

String[] strExec = new String[] 
{ 
       "/system/bin/sh", "-c","/system/bin/chmod 777
};

try {
    Runtime.getRuntime().exec(strExec);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
0
On

1.Do RootTools.isAccessGiven() check first, then the system will ask for "Root" permission in the style of Dialog. After permitting the root can you successfully run the root command.
2."\""+ MyFilePath+ "\""=> MyFilePath