diff --git a/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt b/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt index 5a66cfde3b..ed4fb76e8b 100644 --- a/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt +++ b/lottie-compose/src/main/java/com/airbnb/lottie/compose/rememberLottieComposition.kt @@ -1,6 +1,7 @@ package com.airbnb.lottie.compose import android.content.Context +import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Typeface import android.util.Base64 @@ -238,15 +239,18 @@ private fun maybeLoadImageFromAsset( Logger.warning("Unable to open asset.", e) return } - try { + val bitmap: Bitmap? = try { val opts = BitmapFactory.Options() opts.inScaled = true opts.inDensity = 160 - var bitmap = BitmapFactory.decodeStream(inputStream, null, opts) - bitmap = Utils.resizeBitmapIfNeeded(bitmap, asset.width, asset.height) - asset.bitmap = bitmap + BitmapFactory.decodeStream(inputStream, null, opts) } catch (e: IllegalArgumentException) { Logger.warning("Unable to decode image.", e) + null + } + + if (bitmap != null) { + asset.bitmap = Utils.resizeBitmapIfNeeded(bitmap, asset.width, asset.height) } } diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java index e309d095ae..57b723c95c 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieCompositionFactory.java @@ -710,8 +710,10 @@ private static LottieResult fromZipStreamSyncInternal(@Nullab return null; } Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); - bitmap = Utils.resizeBitmapIfNeeded(bitmap, asset.getWidth(), asset.getHeight()); - asset.setBitmap(bitmap); + if (bitmap != null) { + bitmap = Utils.resizeBitmapIfNeeded(bitmap, asset.getWidth(), asset.getHeight()); + asset.setBitmap(bitmap); + } } } } diff --git a/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java b/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java index dfd07fa1af..2d735c5751 100644 --- a/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java +++ b/lottie/src/main/java/com/airbnb/lottie/manager/ImageAssetManager.java @@ -106,7 +106,19 @@ public void setDelegate(@Nullable ImageAssetDelegate assetDelegate) { Logger.warning("data URL did not have correct base64 format.", e); return null; } - bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); + + try { + bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); + } catch (IllegalArgumentException e) { + Logger.warning("Unable to decode image `" + id + "`.", e); + return null; + } + + if (bitmap == null) { + Logger.warning("Decoded image `" + id + "` is null."); + return null; + } + Bitmap resizedBitmap = Utils.resizeBitmapIfNeeded(bitmap, asset.getWidth(), asset.getHeight()); return putBitmap(id, resizedBitmap); } diff --git a/lottie/src/main/java/com/airbnb/lottie/utils/Utils.java b/lottie/src/main/java/com/airbnb/lottie/utils/Utils.java index 788c9f3d9c..22a3ec6e51 100644 --- a/lottie/src/main/java/com/airbnb/lottie/utils/Utils.java +++ b/lottie/src/main/java/com/airbnb/lottie/utils/Utils.java @@ -280,7 +280,7 @@ public static float getAnimationScale(@NonNull Context context) { * Resize the bitmap to exactly the same size as the specified dimension, changing the aspect ratio if needed. * Returns the original bitmap if the dimensions already match. */ - public static Bitmap resizeBitmapIfNeeded(Bitmap bitmap, int width, int height) { + public static Bitmap resizeBitmapIfNeeded(@NonNull Bitmap bitmap, int width, int height) { if (bitmap.getWidth() == width && bitmap.getHeight() == height) { return bitmap; }