tags:

views:

24

answers:

2

In my Android app I am calling a URL for fetching images and that URL is also fetched dynamically. Sometimes what happened is that the server returns the path of the image but actually there is no image so can we put some kind of validation.

+1  A: 

The most obvious solution is using the Content-Type the server returns. You can get this from HttpEntity.getContentType(). If it's an image, it should start with "image/". If this is missing or inaccurate, there are workarounds.

Matthew Flaschen
A: 

When the url has finished loading try to decode the result into a Bitmap object with decodeByteArray or decodeStream, if it returns null the image is invalid.

byte[] data; // server response
// ...
Bitmap img = BitmapFactory.decodeByteArray(data, 0, data.length);
if (img == null) {
  // invalid image
}
else {
  // valid image
}
cristis