How to take a screenshots?

2019-01-27 06:33发布

问题:

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

回答1:

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"));