2

I have a file directory on my server with images. The name of the image is associated with an items table. Item ID#1, will have a corresponding image "1.png".

Not all items have an image so if the app checks, it will find nothing on some of them. In that case, I want an if check to show a default image.

My question: with only a URL available, how can I check if the url leads to an image or if it leads to nothing/blank/null?

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • Apart from Android details such as network threading issues, you can find the core of a solution in this Java question: http://stackoverflow.com/questions/1378199/how-to-check-if-a-url-exists-or-returns-404-with-java - while the basic mechanism is to use the getResponseCode() method of HttpURLConnection take time to read all the answers as they point out a variety of potential complications to be aware of. – Chris Stratton May 09 '14 at 22:50

2 Answers2

5

I would suggest asking for the Head of the image request simply because it is faster.

Non Picasso :

HttpClient client= new DefaultHttpClient();
HttpHead headMethod = new HttpHead(urlToImage);
HttpResponse response = client.execute(headMethod);
if(response.getStatusLine().getStatusCode== HttpStatus.SC_NOT_FOUND) {
    // Image does not exist, show a placeholder or something
} else {
  // load image into the view.
}

Picasso : (nothing to do with the head version, but this is an easy alternative to handle it, also look at the Callback that Picasso can take in to fine tune the success/error)

Picasso.with(context).load(urlToImage).into(imageView).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error);

References:

Apache HttpHead from Android's SDK Reference

Apache HttpStatus from Android's SDK Reference

Square Picasso

frederick_c_siu
  • 540
  • 3
  • 8
1

Assuming you are talking about a scenario where you are attempting to fetch the image over HTTP / HTTPS ...

The simple approach is to just attempt to fetch the image. Then you check the response to see what the response status code is and what the content type is. If they 200 and "image/png" respectively then you have the image. If the status code is 404 then the image does not exist. If neither, then it is a reasonable assumption that "something has gone wrong" at the server end.

The linked question has code for doing this kind of thing.


The problem is that you are somewhat dependent on what the server does when you attempt to fetch an image that isn't there. It could plausibly:

  • substitute a default image of its own
  • return an error page, with or without setting a 404 status code
  • redirect you to an error page, which means that the status code would be a 3xx,
  • fail, resulting in a 5xx response, or even
  • seem to succeed but return an empty body, or a body whose actual contents doesn't match the response "content-type" header.

If you implemented the server, or you are dealing directly with the implementers, you can make assumptions about what it will do. But if your code has to work against any server, then it needs to be more complicated ... and more defensive.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216