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

来源:互联网 发布:清华大学软件学院校友 编辑:程序博客网 时间:2024/05/24 04:57
    /**     * 获取指定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();        }    }
0 0
原创粉丝点击