一、去除状态栏截图
//整个手机屏幕的视图 View view = getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); // 获取状态栏高度 Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; Log.i("TAG", "" + statusBarHeight); // 获取屏幕长和高 int width = getWindowManager().getDefaultDisplay().getWidth(); int height = getWindowManager().getDefaultDisplay().getHeight(); Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width, height - statusBarHeight); //保存图片 FileOutputStream fout = null; try { fout = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } b.compress(Bitmap.CompressFormat.PNG, 100, fout); //显示截图 mImageView.setImageBitmap(b);
二、整个手机屏幕截图,但状态栏是空白
View view = getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); FileOutputStream fout = null; try { fout = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test.png"); } catch (FileNotFoundException e) { e.printStackTrace(); } bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout); mImageView.setImageBitmap(bitmap);