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

Image request failed with response code

Error message

Image request failed with response code 

What it means

BaseImageDownloader fires IOException("Image request failed with response code N") when the HTTP response code is not acceptable (shouldBeProcessed returns false — by default any code outside 200-299). The error stream has already been drained to allow connection reuse, then the exception propagates to the ImageLoader pipeline where it reaches onLoadingFailed. It signals an HTTP-level failure (404, 500, 301 without follow, etc.), not a network/IO failure.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/core/download/BaseImageDownloader.java:132

		HttpURLConnection conn = createConnection(imageUri, extra);

		int redirectCount = 0;
		while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
			conn = createConnection(conn.getHeaderField("Location"), extra);
			redirectCount++;
		}

		InputStream imageStream;
		try {
			imageStream = conn.getInputStream();
		} catch (IOException e) {
			// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
			IoUtils.readAndCloseStream(conn.getErrorStream());
			throw e;
		}
		if (!shouldBeProcessed(conn)) {
			IoUtils.closeSilently(imageStream);
			throw new IOException("Image request failed with response code " + conn.getResponseCode());
		}

		return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), conn.getContentLength());
	}

	/**
	 * @param conn Opened request connection (response code is available)
	 * @return <b>true</b> - if data from connection is correct and should be read and processed;
	 *         <b>false</b> - if response contains irrelevant data and shouldn't be processed
	 * @throws IOException
	 */
	protected boolean shouldBeProcessed(HttpURLConnection conn) throws IOException {
		return conn.getResponseCode() == 200;
	}

	/**
	 * Create {@linkplain HttpURLConnection HTTP connection} for incoming URL
	 *

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Verify the URL in a browser/curl — fix the source URI if the resource is gone or moved
  2. Handle auth-required assets by adding headers via a custom ImageDownloader (getStream override) or extraForDownloader credentials
  3. Catch the IOException in your ImageLoadingListener.onLoadingFailed and show a fallback image
  4. If a non-2xx code is genuinely OK in your backend, subclass BaseImageDownloader and override shouldBeProcessed

Example fix

// before
imageLoader.displayImage(photoUrl, imageView, options); // 404 -> onLoadingFailed with IOException

// after
imageLoader.displayImage(photoUrl, imageView, options, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingFailed(String uri, View view, FailReason reason) {
        imageView.setImageResource(R.drawable.placeholder); // graceful fallback
    }
});
Defensive patterns

Strategy: try-catch

Try / catch

imageLoader.displayImage(uri, imageView, options, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingFailed(String uri, View view, FailReason reason) {
        Throwable c = reason.getCause();
        if (c instanceof IOException && c.getMessage() != null
                && c.getMessage().startsWith("Image request failed with response code")) {
            // HTTP-level failure: fix URL or show placeholder, do NOT retry blindly
            ((ImageView) view).setImageResource(R.drawable.error_placeholder);
        } else {
            ((ImageView) view).setImageResource(R.drawable.offline_placeholder);
        }
    }
});

Prevention

When it happens

Trigger: Loading an http(s) URI whose server returns 4xx/5xx (e.g. expired CDN link returning 404, deleted photo returning 500) or a 3xx when redirects are not followed; a custom subclass of BaseImageDownloader overriding shouldBeProcessed with stricter rules; intercepted/proxied responses returning captive-portal codes.

Common situations: Expired or moved image URLs in production; API gateways returning 401/403 for unauthorized asset fetches; dev machines behind corporate proxies returning 502/503; servers returning 304 to a request without conditional headers.

Related errors


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