I'm trying to read an image from the net using ImageIO:
URL url = new URL(location);
bi = ImageIO.read(url);
When location is a URL that ends with the actual image (e.g. http://www.lol.net/1.jpg), the code above works. However, when the URL is a redirection (e.g. http://www.lol.net/redirection, leading to http://www.lol.net/1.jpg), the code above returns null in bi.
Two questions. One, why is this happening? Is it because the ImageIO library tries to find a suitable ImageReader based on the URL string? And two, what will be the cleanest solution to this limitation? Note that I require a BufferedImage output and not an Image output.
EDIT: For whomever wants to test it, the URL I'm trying to read is http://graph.facebook.com/804672289/picture, which is converted into http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs351.snc4/41632_804672289_6662_q.jpg.
EDIT 2: I was incorrect in the last edit. the URL is https://graph.facebook.com/804672289/picture. If I replace the https with http, the code above works fine. So my new question is how to make it work with HTTPS so I won't need to do the replacement.
To me it doesn't seem like http://www.lol.net/1.jpg points directly to an image.
As @Bozho points out, the ImageIO uses the default URL.openConnection
which (since the address starts with "http") returns a HttpURLConnection
which, per default has setFollowRedirects(true)
.
Regarding your edit, this code seems to work just fine to me:
URL url = new URL("http://graph.facebook.com/804672289/picture");
BufferedImage bi = ImageIO.read(url);
System.out.println(bi);
// Prints: BufferedImage@43b09468: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@7ddf5a8f transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 50 height = 50 #numDataElements 3 dataOff[0] = 2
I suspect your error is somewhere else.