Android 2.2 - downloading images from web fails 1 out of 2
<p>I have the following code to download images from my webserver. </p>

<p>Problem is that on Android 2.2 (only 2.2 - works fine in 2.3) only 1 out of 2 downloads actually work out. Every second download does not read anything. I've updated my code to check, and if failed, retry. The retry works fine so it's not related to the partiular image.</p>

<p>My code:</p>

<pre><code>public int DownloadFromUrl(String imageURL, String fileName) {

        int downloadlength=0;
        try {

                URL url = new URL(imageURL); 
                Log.d("ImageManager", "download from " + url + " to " + fileName);
                URLConnection ucon = url.openConnection();
                InputStream is = ucon.getInputStream();


                BufferedInputStream bis = new BufferedInputStream(is, 8192);
                ByteArrayBuffer baf = new ByteArrayBuffer(128);
                int current = 0;


                while ((current = bis.read()) != -1) {
                    baf.append((byte) current);
                }


                downloadlength = baf.length();
                if (downloadlength &gt; 0) {
                    File file = new File(getFilesDir()+"/"+fileName); 
                    Log.d("ImageManager", "file read, " + baf.length() + " bytes, will try storing in "+file);
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(baf.toByteArray());
                    fos.close();
                }
                else {
                    Log.d("ImageManager", "nothing downloaded - not saving");


                }


        } catch (Exception e) {
            Log.d("ImageManager", "Error: " + e);
        }
        return downloadlength;

    }



    private class DownloadImages extends AsyncTask&lt;Integer, Integer, Integer&gt; {

        @Override
        protected Integer doInBackground(Integer... pic_ids) {

            int count = pic_ids.length;
            for (int i=0; i &lt; count; i++) {

                // download the image from website ...
                String url = "http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid="+pic_ids[i];
                Log.d("DownloadImages", "("+i+") downloading url "+url);
                int downloadlength = DownloadFromUrl(url,pic_ids[i]+".jpg");

                if (downloadlength &gt; 0) {

                    Log.d("DownloadImages", "("+i+") download finished &amp; saved "+url);

                }
                else {
                    Log.d("DownloadImages", "donwload failed - retrying");
                    downloadlength = DownloadFromUrl(url,pic_ids[i]+".jpg");

                    if (downloadlength &gt; 0) {
                        Log.d("DownloadImages", "worked fine now ...");
                    }
                    else {
                        Log.d("DownloadImages", "still not downloaded");
                    }
                }

                // this publishes in percent ...
                //publishProgress ((int) (((i+1) / (float) count)*100));
                // publish in number of files instead
                publishProgress((int) (i+1));
            }

            return count;
        }
    }
</code></pre>

<p>The LogCat only contains my Log statements, no errors. 
As you can see, the first file downloads nicely, the second one fails and has to retry. Works fine then. 3rd file and following are the same (every second download works).</p>

<pre><code>07-29 09:40:54.062: INFO/global(13482): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
07-29 09:40:54.652: DEBUG/DownloadImages(13482): (0) downloading url http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892204
07-29 09:40:54.652: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892204 to 892204.jpg
07-29 09:40:55.342: INFO/ActivityManager(71): Displayed activity com.mydomain.app/.SyncOnce: 1923 ms (total 1923 ms)
07-29 09:40:58.822: DEBUG/dalvikvm(13482): GC_FOR_MALLOC freed 3761 objects / 408120 bytes in 59ms
07-29 09:41:00.332: DEBUG/ImageManager(13482): file read, 50817 bytes, will try storing in /data/data/com.mydomain.app/files/892204.jpg
07-29 09:41:00.332: DEBUG/DownloadImages(13482): (0) download finished &amp; saved http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892204
07-29 09:41:00.362: DEBUG/DownloadImages(13482): (1) downloading url http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892203
07-29 09:41:00.392: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892203 to 892203.jpg
07-29 09:41:00.682: DEBUG/ImageManager(13482): nothing downloaded - not saving
07-29 09:41:00.682: DEBUG/DownloadImages(13482): donwload failed - retrying
07-29 09:41:00.692: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892203 to 892203.jpg
07-29 09:41:05.572: DEBUG/ImageManager(13482): file read, 50425 bytes, will try storing in /data/data/com.mydomain.app/files/892203.jpg
07-29 09:41:05.582: DEBUG/DownloadImages(13482): worked fine now ...
07-29 09:41:05.612: DEBUG/DownloadImages(13482): (2) downloading url http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892193
07-29 09:41:05.652: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892193 to 892193.jpg
07-29 09:41:05.852: DEBUG/ImageManager(13482): nothing downloaded - not saving
07-29 09:41:05.852: DEBUG/DownloadImages(13482): donwload failed - retrying
07-29 09:41:05.862: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892193 to 892193.jpg
07-29 09:41:09.382: DEBUG/dalvikvm(13482): GC_FOR_MALLOC freed 3269 objects / 552920 bytes in 45ms
07-29 09:41:17.682: DEBUG/dalvikvm(13482): GC_FOR_MALLOC freed 182 objects / 105440 bytes in 40ms
07-29 09:41:17.692: INFO/dalvikvm-heap(13482): Grow heap (frag case) to 3.607MB for 262160-byte allocation
07-29 09:41:17.852: DEBUG/dalvikvm(13482): GC_FOR_MALLOC freed 1 objects / 131096 bytes in 156ms
07-29 09:41:21.312: DEBUG/ImageManager(13482): file read, 169122 bytes, will try storing in /data/data/com.mydomain.app/files/892193.jpg
07-29 09:41:21.332: DEBUG/DownloadImages(13482): worked fine now ...
07-29 09:41:21.352: DEBUG/DownloadImages(13482): (3) downloading url http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892184
07-29 09:41:21.392: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892184 to 892184.jpg
07-29 09:41:21.552: DEBUG/ImageManager(13482): nothing downloaded - not saving
07-29 09:41:21.552: DEBUG/DownloadImages(13482): donwload failed - retrying
07-29 09:41:21.562: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892184 to 892184.jpg
07-29 09:41:25.782: DEBUG/ImageManager(13482): file read, 40484 bytes, will try storing in /data/data/com.mydomain.app/files/892184.jpg
07-29 09:41:25.782: DEBUG/DownloadImages(13482): worked fine now ...
07-29 09:41:25.832: DEBUG/DownloadImages(13482): (4) downloading url http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892182
07-29 09:41:25.852: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892182 to 892182.jpg
07-29 09:41:26.022: DEBUG/ImageManager(13482): nothing downloaded - not saving
07-29 09:41:26.022: DEBUG/DownloadImages(13482): donwload failed - retrying
07-29 09:41:26.032: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&amp;mode=M&amp;pid=892182 to 892182.jpg
07-29 09:41:29.562: DEBUG/dalvikvm(13482): GC_FOR_MALLOC freed 2134 objects / 747720 bytes in 44ms
07-29 09:41:32.482: DEBUG/ImageManager(13482): file read, 69786 bytes, will try storing in /data/data/com.mydomain.app/files/892182.jpg
07-29 09:41:32.482: DEBUG/DownloadImages(13482): worked fine now ...
</code></pre>

<p>FYI, If I take out the check (if downloadlength > 0) to retry, I'll end up with only file 1, 3, 5 etc downloaded...</p>
