1

So I'm developing an application that will record a devices screen. I am using Runtime.getRuntimeto execute commands.

So I discovered you can use adb shell screenrecord --bit-rate 8000000 --time-limit 15 /sdcard/demovideo.mp4to record you devices screen via adb, so I did this.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageButton time_launch = (ImageButton) findViewById(R.id.r_1);
        time_launch.setOnClickListener(new View.OnClickListener() { 

         @Override
         public void onClick(View v) {
             Process process = null;
            try {
                process = Runtime.getRuntime().exec("screenrecord --bit-rate 8000000 --time-limit 5 /sdcard/video.mp4");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             BufferedReader bufferedReader = new BufferedReader(
             new InputStreamReader(process.getInputStream()));
            }
        });


}
}

But sadly, nothing is happening when I press the button, the video doesnt record at all. I tried looking around on the internet but nothing is helpful. Hopefully one of you guys can help me out.

Robin
  • 577
  • 1
  • 7
  • 13

1 Answers1

0

Try running "su" command before screen record command and see if it works.Something like below:

             @Override
            public void onClick(View v) {
                 Process process = null;
                try {
                    Runtime.getRuntime().exec("su");
                    process = Runtime.getRuntime().exec("screenrecord   --bit-rate    8000000 --time-limit 5 /sdcard/video.mp4");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 BufferedReader bufferedReader = new BufferedReader(
                 new InputStreamReader(process.getInputStream()));
                }
            });

I have seen some problem while using bitrate option..try running the command without bitrate parameter-just give only path parameter process = Runtime.getRuntime().exec("screenrecord /sdcard/video.mp4");...

Also add WRITE_EXTERNAL_STORAGE permission to your app manifest.

This wont work on non-rooted devices.

You can also try the following:

Use reflection to call takeScreenshot() from PhoneWindowManager.java Refer the following code from here to learn how to use reflection to call hidden APIs.\ Do similar thing for PhoneWindowManger.

TelephonyManager telephony = (TelephonyManager) 
  context.getSystemService(Context.TELEPHONY_SERVICE);  
  try {
      Log.v(TAG, "Get getTeleService...");
      Class c = Class.forName(telephony.getClass().getName());
      Method m = c.getDeclaredMethod("getITelephony");
      m.setAccessible(true);
      telephonyService = (ITelephony) m.invoke(telephony);
      telephonyService.silenceRinger();
      Log.v(TAG, "Answering Call now...");
      telephonyService.answerRingingCall();
      Log.v(TAG, "Call answered...");
      //telephonyService.endCall();
  } catch (Exception e) {
   e.printStackTrace();
   Log.e(TAG,
           "FATAL ERROR: could not connect to telephony subsystem");
   Log.e(TAG, "Exception object: " + e);
  }
rupesh jain
  • 3,410
  • 1
  • 14
  • 22
  • I tried the first bit of your answer, after calling su. You get a pop up asking for permissions. But after that. (5 second period). It doesn't record anything? I'm just wondering how are other apps achieving this? – Robin Aug 10 '14 at 04:05
  • @robin I have seen some problem while using bitrate option..try running the command without bitrate parameter-just give only path parameter `process = Runtime.getRuntime().exec("screenrecord /sdcard/video.mp4");Also make sure the file path you are giving is correct. Let us know if this works. Also add WRITE_EXTERNAL_STORAGE permission to your app manifest. – rupesh jain Aug 10 '14 at 05:16
  • it works, I go into my sdcard and it says video.mp4 but the file size is 00.00kb and it doesnt play. Also I ran this code process = Runtime.getRuntime().exec("screenrecord --time-limit 15 /sdcard/video.mp4"); Because I had no clue how I would stop the recording so I added a time limit – Robin Aug 15 '14 at 06:56
  • To stop recording you can simulate Ctrl+C after specific interval..to do it refer-http://stackoverflow.com/questions/2886756/trigger-os-to-copy-ctrlc-or-ctrl-x-programmatically..see if it works now... – rupesh jain Aug 15 '14 at 14:13
  • @Robin also check whether running screen record from command prompt works..just to figure out whether there is something wrong with the device.. – rupesh jain Aug 15 '14 at 14:14