//截取屏幕的方法 private void screenCut() { // 获取屏幕 // View dView = getWindow().getDecorView();//截取全屏,获取需要截取的控件,获取Activity整个窗口最顶层的View View dView = image;//截取其中某个控件的部分 dView.setDrawingCacheEnabled(true);//设置控件允许绘制缓存 dView.buildDrawingCache(); Bitmap bmp = dView.getDrawingCache();//获取控件的绘制缓存 if (bmp != null) { try { // 获取内置SD卡路径 String sdCardPath = Environment.getExternalStorageDirectory().getPath(); // 图片文件路径 String imagePath = sdCardPath + File.separator + "abc.png"; File file = new File(imagePath); FileOutputStream os = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); } catch (Exception e) { } } }
// 禁止截屏 ,设置这个标记后,在这个Activity界面,无论是系统截屏,还是adb命令获取截屏,都将无法使用(有root权限的不正常情况除外)。
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);