直接上代码:
入口方法: startTakeScreenShot()
出口方法:返回的Bitmap对象在onActivityResult() 有todo的注释
注意:该截屏方式 在 build.gradle属性 stargetSdk >=30 的情况下会报错。我这边是28。
val EVENT_SCREENSHOT = 22 //截图事件private var mediaProjectionManager: MediaProjectionManager? = nullprivate var mediaProjection: MediaProjection? = nullprivate var image: Image? = null// 开始截屏方法fun startTakeScreenShot() {mediaProjectionManager =application.getSystemService(MEDIA_PROJECTION_SERVICE) as MediaProjectionManagerstartActivityForResult(mediaProjectionManager!!.createScreenCaptureIntent(),EVENT_SCREENSHOT)}@SuppressLint("WrongConstant")override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_PIC) {if (data != null) {val pathList: List<String>? =data.getStringArrayListExtra(Repair_ImgSelActivity.INTENT_RESULT)doPicDataS(pathList)}}if (requestCode == EVENT_SCREENSHOT) {super.onActivityResult(requestCode, resultCode, data)Log.e("whh0914", "captureScreen...")val displayMetrics = DisplayMetrics()val windowManager = this.getSystemService(WINDOW_SERVICE) as WindowManagerwindowManager.defaultDisplay.getMetrics(displayMetrics)val width = displayMetrics.widthPixelsval height = displayMetrics.heightPixelsval mImageReader: ImageReader =ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2)mediaProjection = mediaProjectionManager?.getMediaProjection(resultCode, data!!)val virtualDisplay = mediaProjection?.createVirtualDisplay("screen-mirror",width,height,displayMetrics.densityDpi,DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,mImageReader.surface,null,null)Handler().postDelayed(Runnable {try {image = mImageReader.acquireLatestImage()if (image != null) {val planes: Array<Image.Plane> = image!!.planesval buffer: ByteBuffer = planes[0].bufferval width: Int = image!!.widthval height: Int = image!!.heightval pixelStride: Int = planes[0].pixelStrideval rowStride: Int = planes[0].rowStrideval rowPadding = rowStride - pixelStride * widthvar bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride,height,Bitmap.Config.ARGB_8888)bitmap!!.copyPixelsFromBuffer(buffer)bitmap =Bitmap.createScaledBitmap(bitmap, bitmap.width, bitmap.height, false)val pipelineFragment = viewPagerAdapter.fragmentList[1] as PipelineFragmentpipelineFragment.mWebView.visibility = View.GONEif (bitmap != null) {// 这里获取到截到的Bitmap对象// 下面是将bitmap保存到SD卡中的方法:如有需要请参考博客:/qq_39731011/article/details/123640342 如不需要请删除ImageSaveUtil.saveAlbum(this,bitmap,pressFormat.JPEG, 80, true)}bitmap.recycle()}} catch (e: java.lang.Exception) {Log.e("截图", "出现异常:$e")} finally {if (image != null) {image!!.close()}if (mImageReader != null) {mImageReader.close()}virtualDisplay?.release()//该代码为了避免出现BufferQueueProducer: [ImageReader] dequeueBuffer: BufferQueue has been abandonedmImageReader.setOnImageAvailableListener(null, null)mediaProjection?.stop()}}, 300)//这里的延迟是为了 不截取到 系统提示是否允许录屏的弹框,可自行调整,经深海测试一般100-500为最佳}}
如果觉得《Android WebView截屏空白或者一片黑如何解决?使用MediaProjection截图。》对你有帮助,请点赞、收藏,并留下你的观点哦!