3

I have bunch of images from dozen or so sources and I am downloading them in background thread. Most of images are downloaded without problems but there are 2 sources that are causing problems. All images from them fail to load.

I am using following code to download (and store) images:

File f=new File(cacheDir, urlHash);
Bitmap bitmap=null;
InputStream is=new URL(url).openStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, null);

Utils.CopyStream :

public static void CopyStream(InputStream is, OutputStream os)
{
    int counter = 0;
    final int buffer_size=1024;
    try
    {
        byte[] bytes=new byte[buffer_size];
        for(;;)
        {
          int count=is.read(bytes, 0, buffer_size);
          if(count==-1) {
              Log.d("tag", counter + " bytes copied");
              break;
          }
          os.write(bytes, 0, count);
          counter += count;
        }
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
}

when I try to download this image http://www.zapakatel.cz/static/deal/7193-1057b.jpg than it fails. BitmapFactory.decodeStream returns NULL. All I could find out about this that could cause a problem is that is.bytesRemaining is missing few thousands bytes: 162721 vs 179845 according to downloaded file size. When I manually download image from that url and upload it somewhere else, than everything works just fine.

Any idea what could cause problems? Is it possible that server hosting this image cuts me out before download is finished? This image load fine on any PC and even in my iphone app (I know that this is completely different platform and that it probably isn't important, I just find it weird)

2
  • Is there any particular reason you are not using buffered streams?
    – dave.c
    Feb 2, 2011 at 14:57
  • I tried that too, with same results
    – Lope
    Feb 2, 2011 at 14:58

1 Answer 1

6

I have absolutely no experience with Android, but this problem seems to be related to GZIP encoding of the response.

The file you pointed to is returned with GZIP encoding applied. 162721 bytes is the size of encoded data, 179845 is the size after decoding, you can check it with any HTTP inspector. It looks like new URL(url).openStream() doesn't decode GZIP-encoded responses automatically.

2
  • 1
    wow, I had no idea that something like this was possible. You were right. Now I chceck if content is GZIP-encoded and if so I use GZIPInputStream. Everything works like charms... thanks a lot!
    – Lope
    Feb 2, 2011 at 15:16
  • 1
    And that's only one reason NOT to use URL.openStream. It will also use an indefinite (forever) timeout -- meaning if the host isn't there it will sit there waiting. For robust and complete HTTP handling use the HttpClient stuff Android includes. Feb 2, 2011 at 21:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.