android 11以上 截图工具类

android 11以上 截图工具类

以上方法,需要系统权限才能使用,而且必须集成系统的API才能使用。并非常规应用可以使用的。需要重要区分。例如:负一屏截图等。

工具类

kotlin 复制代码
package com.ivi.system.utils


import android.content.Context
import android.graphics.Bitmap
import android.graphics.Rect
import android.os.ServiceManager
import android.util.Log
import android.view.Display
import android.view.IWindowManager
import android.view.WindowManager
import android.view.WindowMetrics
import android.window.ScreenCapture
import com.ivi.base.utils.LogUtil


object ScreenShotUtil {
    private const val TAG = "ScreenShotUtil"
    private const val WIDTH = 1920
    private const val HEIGHT = 1080

    private var bmp: Bitmap? = null
    private var sourceCrop: Rect? = null
    private var captureArgs: ScreenCapture.CaptureArgs? = null

    private var windowManager: WindowManager? = null
    private var iWindowManager: IWindowManager? = null

    private var display: Display? = null

    private var mContext: Context? = null

    fun init(context: Context) {
        mContext = context
        windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

        iWindowManager = IWindowManager.Stub.asInterface(
            ServiceManager.getServiceOrThrow(Context.WINDOW_SERVICE)
        )

        val windowMetrics: WindowMetrics? = windowManager?.currentWindowMetrics

        display = context.display

        var width = windowMetrics?.bounds?.width()

        var height = windowMetrics?.bounds?.height()

        if (width == null)
            width = WIDTH
        if (height == null)
            height = HEIGHT

        sourceCrop = Rect(0, 0, width, height)
        captureArgs = ScreenCapture.CaptureArgs.Builder()
            .setSourceCrop(sourceCrop)
            .build()

    }

    fun screenshot(): Bitmap? {
        LogUtil.i(TAG, "screenshot: ")
        try {

            Log.d(TAG, "screenshot: start")

            val syncScreenCapture =
                ScreenCapture.createSyncCaptureListener() as ScreenCapture.SynchronousScreenCaptureListener
            display?.displayId?.let {
                iWindowManager?.captureDisplay(
                    it,
                    captureArgs,
                    syncScreenCapture
                )
            }

            val buffer: ScreenCapture.ScreenshotHardwareBuffer? = syncScreenCapture.buffer

            bmp = buffer?.asBitmap()

            Log.d(TAG, "screenshot: end")
            return bmp

        } catch (e: Exception) {
            LogUtil.e(TAG, "screenshot: Exception = $e")
        }
        return null
    }

    fun destroy() {
        windowManager = null
        iWindowManager = null
        display = null
    }

}

使用方法

kotlin 复制代码
ScreenShotUtil.init(context)
val bp = ScreenShotUtil.screenshot()
相关推荐
Mr_pyx1 分钟前
MySQL性能优化:深入理解索引原理与查询优化实战
android
恋猫de小郭4 分钟前
Flutter 凉了没?Flutter 2026 的未来行程和规划,一些有趣的变化
android·前端·flutter
帅次5 分钟前
Android 高级工程师专题深挖:WebView、Context 与初始化链
android·binder·webview·zygote·web app·dalvik
y小花7 分钟前
安卓音频低延时与AAudio
android·音视频
Jwest20218 分钟前
佳维视工业安卓一体机在医生移动查房车中的应用
android
大龄程序员狗哥21 分钟前
第49篇:TensorFlow Lite实战——将图像分类模型部署到安卓手机(项目实战)
android·分类·tensorflow
BetterNow.23 分钟前
安卓内存Previous为什么可以算进freeRam
android·linux·安卓·安卓性能·安卓内存
码云数智-园园28 分钟前
PHP 8.x 命名的参数与属性(Attribute):告别注释,构建真正的元数据
android·ide·android studio
0pen128 分钟前
ZygiskNext 源码解析(三):zygiskd 的模块管理、memfd 与 companion
android·安全·开源
Android_xiong_st28 分钟前
(原创)2026安卓面试复盘
android·面试·职场和发展