From 9bad5f506ca53bcefa39bab31f207c1e967d2e87 Mon Sep 17 00:00:00 2001 From: TonyJiangWJ Date: Thu, 20 Jun 2024 11:01:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=88=AA=E5=9B=BE=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/image/capture/GlobalScreenCapture.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/autojs/src/main/java/com/stardust/autojs/core/image/capture/GlobalScreenCapture.java b/autojs/src/main/java/com/stardust/autojs/core/image/capture/GlobalScreenCapture.java index 5c8584d2..34edc226 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/image/capture/GlobalScreenCapture.java +++ b/autojs/src/main/java/com/stardust/autojs/core/image/capture/GlobalScreenCapture.java @@ -200,10 +200,15 @@ public class GlobalScreenCapture { private void grantMediaProjection() { try { + MediaProjection newMediaProjection = mProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) mData.clone()); + if (newMediaProjection == null) { + Log.d(TAG, "grantMediaProjection: 获取新projection失败"); + return; + } if (mMediaProjection != null) { mMediaProjection.stop(); } - mMediaProjection = mProjectionManager.getMediaProjection(Activity.RESULT_OK, (Intent) mData.clone()); + mMediaProjection = newMediaProjection; } catch (Exception e) { Log.d(TAG, "grantMediaProjection: 获取新projection失败 可能只是MIUI的bug " + e); release(); @@ -212,6 +217,12 @@ public class GlobalScreenCapture { @SuppressLint("WrongConstant") private void initVirtualDisplay(int width, int height, int screenDensity) { + if (mMediaProjection == null) { + grantMediaProjection(); + if (mMediaProjection == null) { + throw new IllegalStateException("mediaProjection 初始化失败,无法刷新"); + } + } Log.d(TAG, "initVirtualDisplay: width:" + width + ",height:" + height + ",density:" + screenDensity); mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 3); try {