Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE a Bitmap reference at the Utils.resizeBitmapIfNeeded #2573

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading