1

I am developing an app which is able to take screen shots and I found the following code which allows me to do that

Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream  os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();

It saves the image with image name I can change it in code but I want the current date and time as the name. I tried puttinge this:

Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());

But after putting formattedDate at the place of image it does not work

Mike
  • 2,132
  • 3
  • 20
  • 33
user2304058
  • 99
  • 2
  • 3
  • 7
  • 1
    what doesn't work? capturing the image or changing the filename? – Rachel Gallen May 24 '13 at 18:24
  • i dont know its really irritating i just did this "/sdcard/"+formattedDate+".png" and it stopped taking screenshots – user2304058 May 24 '13 at 18:25
  • take a look at this blog http://dharmendra4android.blogspot.ie/2012/04/save-captured-image-to-applications.html get the date and then where he says "new image" just substitute formatted date – Rachel Gallen May 24 '13 at 18:28

1 Answers1

2

in some cases having a ":" character in the filename is not allowed. You might want to replace it with "." or "-".

EDIT:

Try this:

os.write(("/system/bin/screencap -p " + "\"/sdcard/"+formattedDate+".png\"").getBytes("ASCII"));
Sam
  • 3,413
  • 1
  • 18
  • 20
  • Can you elaborate on what you mean by "not working" ... Is there an error? or no error and no filesave? – Sam May 24 '13 at 18:39
  • Updated answer. On closer inspection it seems to be an issue with spaces in your shell command. – Sam May 24 '13 at 18:44
  • 1
    Thanks everyone it working now what i did is i changed SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd-hh-mm-ss"); and then i did String st = formattedDate.toString(); os.write(("/system/bin/screencap -p " + "/sdcard/"+st+".png").getBytes("ASCII")); – user2304058 May 24 '13 at 18:48
  • yes this worked because you removed the spaces. if you want spaces just encapsulate the output directory path in double quotes as i have done above and it will work. – Sam May 24 '13 at 18:55
  • Can anyone tell me how I can get an inverted screenshot? – Susheel Apr 22 '15 at 21:10