From 8e2eda15ce5b1637a81d9889ffbfa752893dd86b Mon Sep 17 00:00:00 2001 From: Koji Wakamiya Date: Mon, 8 Dec 2025 19:43:39 +0900 Subject: [PATCH] refactor: Code cleanup --- lib/src/image_converter_android.dart | 78 ++++++++++++---------------- 1 file changed, 32 insertions(+), 46 deletions(-) diff --git a/lib/src/image_converter_android.dart b/lib/src/image_converter_android.dart index 1a4febc..117a386 100644 --- a/lib/src/image_converter_android.dart +++ b/lib/src/image_converter_android.dart @@ -34,60 +34,46 @@ final class ImageConverterAndroid implements ImageConverterPlatform { OutputFormat format = OutputFormat.jpeg, int quality = 100, }) async { - final inputJBytes = JByteArray.from(inputData); + JByteArray? inputJBytes; + Bitmap? bitmap; + Bitmap$CompressFormat? compressFormat; + ByteArrayOutputStream? outputStream; + JByteArray? outputJBytes; try { - final bitmap = BitmapFactory.decodeByteArray( - inputJBytes, - 0, - inputData.length, - ); + inputJBytes = JByteArray.from(inputData); + bitmap = BitmapFactory.decodeByteArray(inputJBytes, 0, inputData.length); if (bitmap == null) { throw Exception('Failed to decode image. Invalid image data.'); } - try { - final compressFormat = switch (format) { - OutputFormat.jpeg => Bitmap$CompressFormat.JPEG, - OutputFormat.png => Bitmap$CompressFormat.PNG, - OutputFormat.webp => Bitmap$CompressFormat.WEBP_LOSSY, - OutputFormat.heic => throw UnsupportedError( - 'HEIC output format is not supported on Android.', - ), - }; + compressFormat = switch (format) { + OutputFormat.jpeg => Bitmap$CompressFormat.JPEG, + OutputFormat.png => Bitmap$CompressFormat.PNG, + OutputFormat.webp => Bitmap$CompressFormat.WEBP_LOSSY, + OutputFormat.heic => throw UnsupportedError( + 'HEIC output format is not supported on Android.', + ), + }; - try { - final outputStream = ByteArrayOutputStream(); - try { - final success = bitmap.compress( - compressFormat, - quality, - outputStream, - ); - if (!success) { - throw Exception('Failed to compress bitmap.'); - } - - final outputJBytes = outputStream.toByteArray(); - if (outputJBytes == null) { - throw Exception('Failed to get byte array from output stream.'); - } - try { - final outputList = outputJBytes.toList(); - return Uint8List.fromList(outputList); - } finally { - outputJBytes.release(); - } - } finally { - outputStream.release(); - } - } finally { - compressFormat.release(); - } - } finally { - bitmap.release(); + outputStream = ByteArrayOutputStream(); + final success = bitmap.compress(compressFormat, quality, outputStream); + if (!success) { + throw Exception('Failed to compress bitmap.'); } + + outputJBytes = outputStream.toByteArray(); + if (outputJBytes == null) { + throw Exception('Failed to get byte array from output stream.'); + } + + return Uint8List.fromList(outputJBytes.toList()); } finally { - inputJBytes.release(); + inputJBytes?.release(); + bitmap?.recycle(); + bitmap?.release(); + compressFormat?.release(); + outputStream?.release(); + outputJBytes?.release(); } } }