ahota ahota - 1 year ago 117
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:


This also does not work, leading me to believe that the problem lies elsewhere. When I log the
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download