/**
* 获取指定Activity的截屏,保存到jpeg文件
*/
public Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView
().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight,
width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
/**
* 保存为图片文件
* @param b {@link Bitmap}
* @param filePath
* @param fileName
*/
public void savePic(Bitmap b, String filePath, String fileName)
{
File f = new File(filePath);
if (!f.exists()) {
f.mkdirs();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath +File.separator + fileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.JPEG,90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
android 截屏并将其保存成jpeg图片格式
最新推荐文章于 2024-04-09 03:21:11 发布