Skip to content

连续截屏java.lang.NullPointerException: it.acquireLatestImage() must not be null #25

@chxip

Description

@chxip

fun takeScreenshot2Bitmap(): Bitmap? {
imageReader?.let {
val image: Image = it.acquireLatestImage()
try {
val bitmap = imageToBitmap(image)
return bitmap
} catch (e: Throwable) {
return null
} finally {
image.close()
}
} ?: let { throw RuntimeException("Please request permission for screen recording first") }
}

连续截屏时acquireLatestImage会来不及填充下一帧,返回null,此处会NullPointerException
建议改成:
fun takeScreenshot2Bitmap(): Bitmap? {
imageReader?.let {
var image: Image? = null
try {
image = it.acquireLatestImage()
val bitmap = imageToBitmap(image)
return bitmap
} catch (e: Throwable) {
return null
} finally {
image?.close()
}
} ?: let { throw RuntimeException("Please request permission for screen recording first") }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions