Skip to content

Commit

Permalink
Fix NPE a Bitmap reference at the Utils.resizeBitmapIfNeeded (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunyh authored Nov 11, 2024
1 parent 3965575 commit 255352b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,10 @@ private static LottieResult<LottieComposition> 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion lottie/src/main/java/com/airbnb/lottie/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 255352b

Please sign in to comment.