我正在开发一个能够截取屏幕截图的应用程序,我发现了以下允许我执行此操作的代码
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();
它用图像名称保存图像我可以在代码中更改它,但我想要当前日期和时间作为名称。我试过这个:
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedDate = df.format(c.getTime());
但是在将 formattedDate 放在图像的位置后它不起作用
最佳答案
在某些情况下,不允许在文件名中包含“:”字符。您可能希望将其替换为“.”。或“-”。
编辑:
试试这个:
os.write(("/system/bin/screencap -p " + "\"/sdcard/"+formattedDate+".png\"").getBytes("ASCII"));
关于android - 如何截取屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741302/