From e25eadc566f3a5f22965d2844e6f73929592832b Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 4 Aug 2025 12:42:32 +0100 Subject: [PATCH] fix: resolve bitmap recycling issue in long screenshot capture MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create bitmap copy in ScreenCaptureService before passing to LongScreenshotCapture - Handle null bitmap.config with fallback to ARGB_8888 - Add proper bitmap cleanup in processBitmapAsync with finally block - Recycle bitmap copy in LongScreenshotCapture after processing - Clean up bitmap copy if capture fails This fixes the "Can't compress a recycled bitmap" error by ensuring the bitmap passed to LongScreenshotCapture is not recycled by the calling service before async processing completes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../pokegoalshelper/ScreenCaptureService.kt | 11 ++++++++--- .../pokegoalshelper/capture/LongScreenshotCapture.kt | 7 +++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt b/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt index 88e0c46..c5eee66 100644 --- a/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt +++ b/app/src/main/java/com/quillstudios/pokegoalshelper/ScreenCaptureService.kt @@ -1328,13 +1328,18 @@ class ScreenCaptureService : Service() { val bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888) Utils.matToBitmap(mat, bitmap) - // Pass the bitmap to the long screenshot system - val captured = longScreenshotCapture?.captureFrame(bitmap) ?: false + // Create a copy of the bitmap for long screenshot processing + val bitmapCopy = bitmap.copy(bitmap.config ?: Bitmap.Config.ARGB_8888, false) + + // Pass the bitmap copy to the long screenshot system + val captured = longScreenshotCapture?.captureFrame(bitmapCopy) ?: false if (!captured) { PGHLog.w(TAG, "⚠️ Failed to capture long screenshot frame") + // Clean up the copy if capture failed + bitmapCopy.recycle() } - // Clean up resources + // Clean up original resources bitmap.recycle() mat.release() } else { diff --git a/app/src/main/java/com/quillstudios/pokegoalshelper/capture/LongScreenshotCapture.kt b/app/src/main/java/com/quillstudios/pokegoalshelper/capture/LongScreenshotCapture.kt index 8fda498..e52b878 100644 --- a/app/src/main/java/com/quillstudios/pokegoalshelper/capture/LongScreenshotCapture.kt +++ b/app/src/main/java/com/quillstudios/pokegoalshelper/capture/LongScreenshotCapture.kt @@ -321,6 +321,13 @@ class LongScreenshotCapture( PGHLog.e(TAG, "❌ Error processing bitmap", e) errorCallback?.invoke("Failed to save screenshot: ${e.message}") } + finally + { + // Always recycle the bitmap after processing + if (!bitmap.isRecycled) { + bitmap.recycle() + } + } }