nostra13/Android-Universal-Image-Loader · error · IllegalArgumentException

ImageAware should wrap ImageView. ImageViewAware is expected

Error message

ImageAware should wrap ImageView. ImageViewAware is expected.

What it means

RoundedBitmapDisplayer.display() requires an ImageViewAware target because RoundedDrawable must be attached to an ImageView through setImageDrawable. Any other ImageAware implementation triggers IllegalArgumentException at display time, after the bitmap has already been downloaded and decoded. The check exists because rounded corners are a view-level visual effect that only an ImageView-backed aware can render.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/core/display/RoundedBitmapDisplayer.java:58

 */
public class RoundedBitmapDisplayer implements BitmapDisplayer {

	protected final int cornerRadius;
	protected final int margin;

	public RoundedBitmapDisplayer(int cornerRadiusPixels) {
		this(cornerRadiusPixels, 0);
	}

	public RoundedBitmapDisplayer(int cornerRadiusPixels, int marginPixels) {
		this.cornerRadius = cornerRadiusPixels;
		this.margin = marginPixels;
	}

	@Override
	public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
		if (!(imageAware instanceof ImageViewAware)) {
			throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
		}

		imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, margin));
	}

	public static class RoundedDrawable extends Drawable {

		protected final float cornerRadius;
		protected final int margin;

		protected final RectF mRect = new RectF(),
				mBitmapRect;
		protected final BitmapShader bitmapShader;
		protected final Paint paint;

		public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {
			this.cornerRadius = cornerRadius;
			this.margin = margin;

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Use an ImageViewAware (or pass the ImageView directly) as the target when rounded corners are enabled
  2. For prefetch/processing-only loads, use a separate DisplayImageOptions without the displayer
  3. Write a custom displayer if you must apply rounding to a non-ImageView target

Example fix

// before
DisplayImageOptions opts = new DisplayImageOptions.Builder()
        .displayer(new RoundedBitmapDisplayer(20))
        .build();
imageLoader.displayImage(uri, new NonViewAware(uri, size, ViewScaleType.CROP), opts);

// after
DisplayImageOptions roundedOpts = new DisplayImageOptions.Builder()
        .displayer(new RoundedBitmapDisplayer(20))
        .build();
imageLoader.displayImage(uri, imageView, roundedOpts);
Defensive patterns

Strategy: type-guard

Validate before calling

boolean isViewTarget = aware instanceof ImageViewAware;
DisplayImageOptions opts = isViewTarget ? roundedOptions : plainOptions;
imageLoader.displayImage(uri, aware, opts);

Type guard

private static boolean supportsDisplayer(ImageAware aware, BitmapDisplayer displayer) {
    return !(displayer instanceof RoundedBitmapDisplayer) || aware instanceof ImageViewAware;
}

Prevention

When it happens

Trigger: Using .displayer(new RoundedBitmapDisplayer(radius, margin)) in DisplayImageOptions and then calling displayImage with a NonViewAware, a custom ImageAware subclass, or an aware wrapping a non-ImageView View. Also hit when the same DisplayImageOptions instance (built for a list of ImageViews) is reused for a non-view prefetch call.

Common situations: Reusing one global DisplayImageOptions object across both ImageView loading and background prefetching; upgrading old code that used ImageLoadingListener-onLoadingComplete with ImageAware; copying the 'rounded corners' sample into a ViewHolder that wraps views in custom awares.

Related errors


AI-assisted analysis of nostra13/Android-Universal-Image-Loader@ba33ec64d0 (2026-08-14). Data as JSON: /api/errors/ded77b1572c6d4fd. Report an issue: GitHub.