bumptech/glide · error · IllegalArgumentException
Loop count must be greater than 0, or equal to GlideDrawable
Error message
Loop count must be greater than 0, or equal to GlideDrawable.LOOP_FOREVER, or equal to GlideDrawable.LOOP_INTRINSIC
What it means
IllegalArgumentException thrown by GifDrawable.setLoopCount() when the value is <= 0 and is neither LOOP_FOREVER nor LOOP_INTRINSIC. Loop counts must be a positive integer or one of the two named sentinels; zero and negative values (other than the sentinels) are rejected to avoid an infinite/no-frame playback configuration.
Source
Thrown at library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java:388
return state;
}
/** Clears any resources for loading frames that are currently held on to by this object. */
public void recycle() {
isRecycled = true;
state.frameLoader.clear();
}
// For testing.
boolean isRecycled() {
return isRecycled;
}
// Public API.
@SuppressWarnings("WeakerAccess")
public void setLoopCount(int loopCount) {
if (loopCount <= 0 && loopCount != LOOP_FOREVER && loopCount != LOOP_INTRINSIC) {
throw new IllegalArgumentException(
"Loop count must be greater than 0, or equal to "
+ "GlideDrawable.LOOP_FOREVER, or equal to GlideDrawable.LOOP_INTRINSIC");
}
if (loopCount == LOOP_INTRINSIC) {
int intrinsicCount = state.frameLoader.getLoopCount();
maxLoopCount =
(intrinsicCount == TOTAL_ITERATION_COUNT_FOREVER) ? LOOP_FOREVER : intrinsicCount;
} else {
maxLoopCount = loopCount;
}
}
/**
* Register callback to listen to GifDrawable animation end event after specific loop count set by
* {@link GifDrawable#setLoopCount(int)}.
*
* <p>Note: This will only be called if the Gif stop because it reaches the loop count. UnregisterView on GitHub (pinned to eb14a895d8)
Solutions
- Use GifDrawable.LOOP_FOREVER or LOOP_INTRINSIC instead of magic numbers.
- Validate the value before calling setLoopCount: clamp to >= 1 or use a sentinel.
- If 'play once' is desired, call setLoopCount(1).
Example fix
// before gifDrawable.setLoopCount(userLoops); // userLoops may be 0 // after int loops = userLoops <= 0 ? GifDrawable.LOOP_INTRINSIC : userLoops; gifDrawable.setLoopCount(loops);
Defensive patterns
Strategy: validation
Validate before calling
// Clamp user-supplied loop counts before calling setLoopCount.
int safeLoops;
if (userLoops == GifDrawable.LOOP_FOREVER || userLoops == GifDrawable.LOOP_INTRINSIC) {
safeLoops = userLoops;
} else {
safeLoops = Math.max(1, userLoops);
}
gifDrawable.setLoopCount(safeLoops); Prevention
- Use the LOOP_FOREVER / LOOP_INTRINSIC sentinels instead of magic numbers.
- Clamp external loop-count inputs to >= 1.
- Treat 0 as LOOP_INTRINSIC at the call site.
When it happens
Trigger: Calling gifDrawable.setLoopCount(0) or setLoopCount(-5). Passing a user-controlled loop count without validation. Computing a loop count that underflows to 0.
Common situations: UI controls that let users set loops and send 0 meaning 'none'. Misreading LOOP_INTRINSIC as 0. Storing loop count in an Int that defaults to 0 and passing it through.
Related errors
- Package name for {source} is null or empty
- Unrecognized Uri format: {source}
- Failed to find resource id for: {source}
- Cannot apply transformation on width: {outWidth} or height:
- Unable to convert {drawable} to a Bitmap
AI-assisted analysis of bumptech/glide@eb14a895d8 (2026-08-14).
Data as JSON: /api/errors/cee5bc78826fca36.
Report an issue: GitHub.