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.
views:
24answers:
2
+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
2010-06-23 07:42:49
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
2010-06-23 08:06:43