/** * 截屏工具类 * @author ooxx * */ public class ScreenShot { // 获取指定Activity的截屏,保存到png文件 private static 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 bitmap = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return bitmap; } // 保存 private static boolean savePic(Bitmap bitmap, String savePath) { FileOutputStream fos = null; boolean success = false; try { fos = new FileOutputStream(savePath); if (null != fos) { success = bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.flush(); fos.close(); } return success; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return success; } /** * 截屏 * 注意添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> * * @param activity 保存的activity * @param savePath 文件的完整路径 * @return 成功true 失败false */ public static boolean shoot(Activity activity, String savePath) { return ScreenShot.savePic(ScreenShot.takeScreenShot(activity), savePath); } }
原文地址 http://www.apkway.com/forum.php?mod=viewthread&tid=3225&extra=page%3D1