0

I need to take screenshots of main screen through my application. I am using the following code but it gives an image of 0Bytes. Please let me know that what I am doing wrong .

  public OnClickListener captureOnClickListener = new OnClickListener(){
  @Override       
  public void onClick(View v) { 
  Process process;            
  try {
  process = Runtime.getRuntime().exec("screencap -p /sdcard/screen_1.png");
  BufferedReader bufferedReader = new BufferedReader(
  new InputStreamReader(process.getInputStream()));

  String line=null;
  System.out.println("<OUTPUT>");
  while((line = bufferedReader.readLine())!= null ){
  System.out.println(line);
   }
  System.out.println("</OUTPUT>");
  int exitVal = process.waitFor();
  System.out.println("Process exitValue :" + exitVal);
  } catch (Exception e) {
  e.printStackTrace();            
  }       
}     

};

Any kind help would be appreciated. Thanks in advance.

  • 2
    Have you tried this answer? http://stackoverflow.com/a/5651242/2777098 – display name Mar 27 '14 at 13:28
  • try this [solution](http://stackoverflow.com/questions/2661536/how-to-programatically-take-a-screenshot-on-android) – Siruk Viktor Mar 27 '14 at 13:29
  • Thanks for your suggestions but i have already tried the code referred by you.It gives the screen shot of my current activity but i want the screenshot of whatever content on the main screen.Any other method.. – DeepThinker Mar 27 '14 at 14:04
  • 1
    @DeepThinker - you aren't allowed to do that. – Chris Stratton Mar 27 '14 at 14:49
  • 3
    The answer linked by @IsabelHM tells you how to do everything that you are allowed to do. Grabbing screen shots from other apps would allow malware to steal passwords (if "show password" is enabled) and capture private conversations, so apps are not allowed to capture screens from other apps. You can work around this on a rooted device or by having the user start a background service from the shell. FWIW, you should be able to see information about screencap failing in the `logcat` output. – fadden Mar 27 '14 at 14:57
  • @ChrisStratton ,@fadden please refer to [this](https://play.google.com/store/apps/details?id=com.enlightment.screenshot) app which is able to take screenshots of device's main screen. Even it works on non-rooted devices as well.I need to develop such an application. – DeepThinker Mar 28 '14 at 04:45
  • On very old versions there may be bugs which can be exploited. On current ones, it's not the app taking the screenshot - the volume down + power screenshot is an interactive feature of android itself which does not involve an app. The app is likely just fooling with the files so produced... – Chris Stratton Mar 28 '14 at 12:02

0 Answers0