0

I am downloading bitmap using this method which I call inside an AsyncTask:

public static Bitmap downloadBitmap(String url) {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);
    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }
        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                System.setProperty("http.keepAlive", "false");
                inputStream = entity.getContent();
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        getRequest.abort();
    } finally {
        if (client != null)
            client.close();
    }
    return null;
}

I have control when connection is lost, however, i don't have any when connection is slow that causes the image to not download completely.

How can i check if the image is not completely downloaded?

Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49
  • Use Async Task for downloading images. Its will help you a lot. – Pravinsingh Waghela Jul 10 '14 at 06:18
  • yep, the method is called inside an asynctask. i updated the question :) – Lendl Leyba Jul 10 '14 at 06:26
  • possible duplicate of [How to check whether the process of download image is completed or not?](http://stackoverflow.com/questions/13965294/how-to-check-whether-the-process-of-download-image-is-completed-or-not) – hungr Jul 10 '14 at 06:37
  • @hungr no it's not a duplicate. my problem is, what i think it is, is that it drops the connection due to slow connection. which results to uncompleted image. but it doesn't drop any error. – Lendl Leyba Jul 11 '14 at 11:18

2 Answers2

0

Try this one: Drawable.createFromPath(filePath)!= null before using it

Nauman Afzaal
  • 1,046
  • 12
  • 20
0

My solution was to put the image filename in a variable and check the file size from the stream and the file downloaded. if it is not a match, i delete the file.

Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49