Giovanni Mariotti Giovanni Mariotti - 7 months ago 94
Java Question

execute shell command from android

I'm trying to execute this command from the application emulator terminal (you can find it in google play) in this app i write

and press enter, so write:

screenrecord --time-limit 10 /sdcard/MyVideo.mp4

and press again enter and start the recording of the screen using the new function of android kitkat.

so, i try to execute the same code from java using this:

Process su = Runtime.getRuntime().exec("su");
Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4");

But don't work because the file is not created. obviously i'm running on a rooted device with android kitkat installed. where is the problem? how can i solve? because from terminal emulator works and in Java not?


You should grab the standard input of the su process just launched and write down the command there, otherwise you are running the commands with the current UID.

Try something like this:

    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

    outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");

}catch(IOException e){
    throw new Exception(e);
}catch(InterruptedException e){
    throw new Exception(e);