1

我正在开发一个能够拍摄屏幕截图的应用程序,我发现下面的代码可以让我做到这一点

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 放在图像的位置后它不起作用

4

1 回答 1

2

在某些情况下,不允许在文件名中包含“:”字符。您可能想用“。”替换它。或者 ”-”。

编辑:

尝试这个:

os.write(("/system/bin/screencap -p " + "\"/sdcard/"+formattedDate+".png\"").getBytes("ASCII"));
于 2013-05-24T18:25:03.217 回答