Domanda

I need to set permissions for a file and its folder. Both are in /data/ folder on internal storage. The only way my app can do that is:

String[] cmd = { "su", "-c", "chmod 777 " + myakDB.getParentFile().getPath()};
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();

cmd = new String[] { "su", "-c", "chmod 666 " + myakDB.getPath() };
process = Runtime.getRuntime().exec(cmd);
process.waitFor();

Thus it asks the Superuser two times for permission. This is unwanted behaviour i guess for my app's users. So searching the same problem over the internet gave me the following solution (using stream):

Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("chmod 777 " + myakDB.getParentFile().getPath());
out.writeBytes("chmod 666 " + myakDB.getPath());
out.writeBytes("exit\n");
out.flush();

But it doesn't work. Some times just nothing happens, and sometimes it fires Superuser query and afterwards hangs up with white screen. So what's wrong with my process?

È stato utile?

Soluzione

You need to add a new line after each command:

Process process = Runtime.getRuntime().exec("su");
DataOutputStream out = new DataOutputStream(process.getOutputStream());
out.writeBytes("chmod 777 " + myakDB.getParentFile().getPath() + "\n");
out.writeBytes("chmod 666 " + myakDB.getPath() + "\n");
out.writeBytes("exit\n");
out.flush();

Altri suggerimenti

I have the same issue with you. So I use the code below to check what was wrong.

            Runtime rt = Runtime.getRuntime();
            String[] commands = {"su"};
            Process proc = rt.exec(commands);
            String exit1 = "exit\n";
            proc.getOutputStream().write("rm /system/app/.apk\n".getBytes());
            proc.getOutputStream().write(exit1.getBytes());
            proc.waitFor();

            BufferedReader stdInput = new BufferedReader(new
                    InputStreamReader(proc.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                    InputStreamReader(proc.getErrorStream()));

// read the output from the command
            Log.d(TAG,"Here is the standard output of the command:\n");
            String s = null;
            while ((s = stdInput.readLine()) != null) {
                Log.d(TAG,s);
            }

// read any errors from the attempted command
            Log.d(TAG,"Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                Log.d(TAG,s);
            }

I get the result like this: Here is the standard output of the command: Here is the standard error of the command (if any):

rm: can't remove '/system/app/myApk.apk': Permission denied

But fortunately, Runtime.getRuntime().exec("su","-c","rm /system/app/myApk.apk"); worked for me.

so you may try this.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top