Question

I have problems while downloading images using HttpClient on Android. It seems like when the image is pushed from the server like "downloadable file" it does not work. On the other hand images displaying right in the browser downloads without any issues. It looks like problem with headers and mimetype of the files provided, or something like that..I thought it was problem with gzip enabled on the server, but after few experiments it definitely doesn't look so. Does anybody know how to solve that? Thanks

Here is brief snippet of my code

HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
Drawable image = Drawable.createFromStream(is, "src"); //the image is null in certain scenarios
Was it helpful?

Solution

Drawable.createFromStream() is not really meant to construct images over a remote HTTP connection. In fact, if you look at the source of this method it ends up calling BitmapFactory.decodeResourceStream(), which is used to create images from the application's internal resources.

I would recommend you try using methods on BitmapFactory directly to decode images coming from the network. Some options would be decodeStream() but sometimes the network can have an issue keeping the buffer full enough for this method. You can also read the bytes from the stream yourself into a byte[] and use decodeByteArray(). You may have better luck with this approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top