public class ScreenShot {
public static String screenShotFolderPath = Environment.getExternalStorageDirectory() + "/Pictures" + "/ScreenShots";
public static String screenShotFilePath =screenShotFolderPath + "/TMFeedbackScreenshot.png";
public static Bitmap screenshotBitmap = null;
public static void shoot(Activity a) {
screenshotBitmap = takeScreenShot(a);
screenShotFilePath=screenShotFolderPath+ "/"+getCurrentTime()+".png";
}
public static void shootToFile(Activity a, File filePath) {
if (filePath == null) {
return;
}
if (!filePath.getParentFile().exists()) {
filePath.getParentFile().mkdirs();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
if (null != fos) {
takeScreenShot(a).compress(Bitmap.CompressFormat.PNG, 100, fos);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static Bitmap takeScreenShot(Activity activity) {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Bitmap b = Bitmap.createBitmap(bitmap);
view.destroyDrawingCache();
return b;
}
private static String getCurrentTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);
return str;
}
}
实现截屏并存储在本地
最新推荐文章于 2024-05-12 12:20:13 发布