ahota ahota - 1 month ago 19
Android Question

Unable to download images using URL on Android

I'm trying to download an image in my Android app using the following couples lines of code

InputStream in = new java.net.URL(url).openStream();
mIcon11 = BitmapFactory.decodeStream(in);


This is apparently a pretty common way to do this, looking at examples. However, it outputs null. I also tried using another library, Picasso:

Picasso.with(context).load("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").into(iv);


This also does not work, leading me to believe that the problem lies elsewhere. When I log the
onImageLoadFailed
listener, I get

com.squareup.picasso.Downloader$ResponseException: 302 Found
at com.squareup.picasso.UrlConnectionDownloader.load(UrlConnectionDownloader.java:96)
at com.squareup.picasso.NetworkRequestHandler.load(NetworkRequestHandler.java:47)


which doesn't make any sense.
I have internet permissions in my Android Manifest. Any ideas what it might be?

Answer

When you put the image url into a browser, it redirects to https://

Change your url in your code to https:// and it should work.

The 302 Found code is used commonly when there is a redirect happening. As is the case here. It is being redirected to the https:// protocol, and a browser would see this and check the data for the redirect url and take you there. Picasso and an InputStream will not check for the redirect code and hence not load the image, but at the same time, not return an error, because it is not a 404 not found response. A good list of HTTP response codes is found on wikipedia.

If you find the image successfully, you will get a 200 OK code returned.