bumptech/glide · error · IllegalArgumentException

Cannot add invalid orientation: {orientation}

Error message

Cannot add invalid orientation: {orientation}

What it means

ExifOrientationStream wraps an InputStream to inject an EXIF orientation byte into the image data. The orientation parameter must be a valid EXIF orientation value in the range [-1, 8], where -1 indicates no orientation and 1-8 are the standard EXIF orientation values. Values outside this range are meaningless and rejected with an IllegalArgumentException at construction time.

Source

Thrown at library/src/main/java/com/bumptech/glide/load/data/ExifOrientationStream.java:71

        0x00,
        0x02,
        /* component count. */
        0x00,
        0x00,
        0x00,
        0x01,
        /* 2 byte orientation value, the first byte of which is always 0. */
        0x00,
      };
  private static final int SEGMENT_LENGTH = EXIF_SEGMENT.length;
  private static final int ORIENTATION_POSITION = SEGMENT_LENGTH + SEGMENT_START_POSITION;
  private final byte orientation;
  private int position;

  public ExifOrientationStream(InputStream in, int orientation) {
    super(in);
    if (orientation < -1 || orientation > 8) {
      throw new IllegalArgumentException("Cannot add invalid orientation: " + orientation);
    }
    this.orientation = (byte) orientation;
  }

  @Override
  public boolean markSupported() {
    return false;
  }

  // No need for synchronized since all we do is throw.
  @SuppressWarnings("UnsynchronizedOverridesSynchronized")
  @Override
  public void mark(int readLimit) {
    throw new UnsupportedOperationException();
  }

  @Override
  public int read() throws IOException {

View on GitHub (pinned to eb14a895d8)

Solutions

  1. Validate the orientation value is in [-1, 8] before constructing ExifOrientationStream
  2. Use ExifInterface#getOrientationAttribute and normalize the value before use
  3. Default to 0 or -1 when the orientation value is unknown or unparseable

Example fix

// before
new ExifOrientationStream(stream, rawOrientation);
// after
int safeOrientation = (orientation < -1 || orientation > 8) ? ExifInterface.ORIENTATION_NORMAL : orientation;
new ExifOrientationStream(stream, safeOrientation);
Defensive patterns

Strategy: validation

Validate before calling

int orientation = readExifOrientation(); // your source
if (orientation < -1 || orientation > 8) {
  orientation = android.media.ExifInterface.ORIENTATION_NORMAL; // default to 1 or 0
}
new ExifOrientationStream(inputStream, orientation);

Prevention

When it happens

Trigger: Creating an ExifOrientationStream with an orientation value read from EXIF metadata that was corrupt or misinterpreted. Passing a raw byte value that exceeds 8. Passing -2 or lower due to a signed/unsigned conversion error.

Common situations: Custom image decoders that read EXIF orientation and construct this stream directly. Incorrect parsing of EXIF orientation integer from image metadata. Unit tests with hardcoded out-of-range values.

Related errors


AI-assisted analysis of bumptech/glide@eb14a895d8 (2026-08-14). Data as JSON: /api/errors/d5cdc68bde7406cb. Report an issue: GitHub.