获取指定Activity的截屏,保存到png文件

    /**
     * 获取指定Activity的Bitmap截屏
     */
    private  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;
        // 获取屏幕长和高
        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, b1.getWidth(), height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }
/**
     * 保存bitmap到sd卡
     * @param bitmap
     * @param strFilePath 文件路径
     *
     */
    private  void savePic(Bitmap bitmap, String strFilePath) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFilePath);
            if (null != fos) {
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
                File file=new File(strFilePath);
                //通知手机图库刷新本地相册
                this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(file)));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(截屏,activity)