microg/GmsCore · error · IllegalArgumentException

width and height must not be negative

Error message

width and height must not be negative

What it means

WebImage's constructor validates image dimensions and throws IllegalArgumentException when width or height is negative. The library requires non-negative pixel dimensions because they are written into a Bundle/Parcel and sent to remote clients, where negative values would be meaningless or corrupt the payload.

Source

Thrown at play-services-base/src/main/java/com/google/android/gms/common/images/WebImage.java:64

     *
     * @param url The URL of the image.
     * @throws IllegalArgumentException If the URL is null or empty.
     */
    public WebImage(Uri url) {
        this(url, 0, 0);
    }

    /**
     * Constructs a new {@link WebImage} with the given URL and dimensions.
     *
     * @param url    The URL of the image.
     * @param width  The width of the image, in pixels.
     * @param height The height of the image, in pixels.
     * @throws IllegalArgumentException If the URL is null or empty, or the dimensions are invalid.
     */
    public WebImage(Uri url, int width, int height) {
        if (url == null) throw new IllegalArgumentException("url cannot be null");
        if (width < 0 || height < 0) throw new IllegalArgumentException("width and height must not be negative");
        this.url = url;
        this.width = width;
        this.height = height;
    }

    /**
     * Gets the image height, in pixels.
     */
    public int getHeight() {
        return height;
    }

    /**
     * Gets the image URL.
     */
    public Uri getUrl() {
        return url;
    }

View on GitHub (pinned to 157c9d86ac)

Solutions

  1. Check width >= 0 and height >= 0 before constructing WebImage
  2. Use 0 as the default/unknown dimension instead of -1
  3. Clamp computed dimensions with Math.max(0, dimension)

Example fix

// before
WebImage img = new WebImage(url, measuredWidth, measuredHeight); // measuredWidth = -1
// after
WebImage img = new WebImage(url, Math.max(0, measuredWidth), Math.max(0, measuredHeight));
Defensive patterns

Strategy: validation

Validate before calling

if (url == null) throw new IllegalArgumentException("url required");
if (width < 0 || height < 0) throw new IllegalArgumentException("dimensions must be >= 0");
WebImage img = new WebImage(url, width, height);

Try / catch

try { new WebImage(url, w, h); } catch (IllegalArgumentException e) { /* fall back to 0x0 or skip */ }

Prevention

When it happens

Trigger: Calling new WebImage(uri, width, height) with a negative width or negative height (e.g. passing an uninitialized or computed -1 sentinel value).

Common situations: Using -1 as a 'not yet measured' default for image size; computing dimensions from a failed bitmap decode or layout measurement that returns -1.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of microg/GmsCore@157c9d86ac (2026-09-06). Data as JSON: /api/errors/80a0f63288752b6c. Report an issue: GitHub.