views:

528

answers:

2

I'm using BitmapFactory.decodeStream to load an image from a url in Android. I want to only download images below a certain size, and I'm currently using getContentLength to check this.

However, I'm told that getContentLength doesn't always provide the size of the file, and in those cases I would like to stop the download as soon as I know that the file is too big. What is the right way to do this?

Here is my current code. I currently return null if getContentLength doesn't provide an answer.

HttpGet httpRequest = new HttpGet(new URL(urlString).toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
final long contentLength = bufHttpEntity.getContentLength();
if ((contentLength >= 0 && (maxLength == 0 || contentLength < maxLength))) {
    InputStream is = bufHttpEntity.getContent();
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    return new BitmapDrawable(bitmap);
} else {
    return null;
}
A: 

You should use HttpHead to issue a HEAD request, which is similar to GET but will return only headers. If the content length is satisfactory, then you can make your GET request to get the content.

The majority of servers should handle HEAD requests without a problem, but occasionally an application servers won't be expecting it and will throw an error. Just something to be aware of.

UPDATE thought I would try to answer your actual question as well. You will probably have to read the data into an intermediate byte buffer before passing the data to the BitmapFactory.

InputStream is = bufHttpEntity.getContent();
ByteArrayOutputStream bytes = ByteArrayOutputStream();
byte[] buffer = new byte[128];
int read;
int totalRead = 0;
while ((read = is.read(buffer)) > 0) {
   totalRead += read;
   if (totalRead > TOO_BIG) {
     // abort download. close connection
     return null;
   }
   bytes.write(buffer, 0 read);
}

Unrelated, but remember to always call consumeContent() on the entity after use so that HttpClient can reuse the connection.

Nick
I know that this doesn't solve your problem of where a server doesn't provide a Content-Length, but I think where you are using HttpGet it might be getting the content whether you want it or not. If it only gets it when you read from the stream, just ignore my answer!
Nick
Thanks Nick. I don't know if using HttpHead would save bandwidth, but in my particular case I prefer to keep the http requests to a minimun.
hgpc
+1  A: 

1 . You can also try this approach. I believe it will not download content for large files

HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();  
int length=connection.getContentLength(); 

if(length<max){  
  InputStream is = connection.getInputStream();  
  BitmapFactory.decodeStream(is,null,null);
}

It's just a sample, you can also add check for -1 and anything else. This approach is equivalent to what you do. It's just one more option for you to try. I just know that HttpURLConnection doesn't fetch content until you start reading it from the stream. So this code will not download large images. I really don't know if HttpClient does the same or not.

2 . Why actually you want to skip larger files. If you're worried about OutOfMemory durung decoding may take a look at this http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966. If you apply inSampleSize you can download even large images. The delay will be larger for larger images but memory consumption will be low. I posted my ListView sample here http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012. It displays images in ListView. There are different size images. Not very big images but anyway.

Fedor
That code does not handle correctly the case when getContentLength returns -1. Also, isn't it a simpler version of what I'm doing already? I want to skip large files because this logic is executed for each item of a ListView.
hgpc
Updated my post with more clarifications.
Fedor
I'm more concerned about the scrolling speed than OutOfMemory. As you mentioned, the delay is larger is you downsize a large image.
hgpc
Image size doesn't affect scrolling speed because image download/decode should be processed in background thread. Take a look at my LazyList sample - it does exactly the same.
Fedor