Android 2.2 - 从网上下载图像失败1

时间:2011-07-29 09:54:33

标签: android

我有以下代码从我的网络服务器下载图像。

问题在于Android 2.2(只有2.2 - 在2.3中正常工作),实际上只有2个下载中的1个可用。每一次下载都不会读取任何内容。我已更新我的代码以进行检查,如果失败,请重试。重试工作正常,因此它与部分图像无关。

我的代码:

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 > 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<Integer, Integer, Integer> {

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

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

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

                if (downloadlength > 0) {

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

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

                    if (downloadlength > 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;
        }
    }

LogCat只包含我的Log语句,没有错误。 如您所见,第一个文件下载得很好,第二个文件失败并且必须重试。工作正常。第三个文件和以下是相同的(每隔一次下载工作)。

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&mode=M&pid=892204
07-29 09:40:54.652: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&mode=M&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 & saved http://www.mysite.com/pic.php?hash=xyz&mode=M&pid=892204
07-29 09:41:00.362: DEBUG/DownloadImages(13482): (1) downloading url http://www.mysite.com/pic.php?hash=xyz&mode=M&pid=892203
07-29 09:41:00.392: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&mode=M&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&mode=M&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&mode=M&pid=892193
07-29 09:41:05.652: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&mode=M&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&mode=M&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&mode=M&pid=892184
07-29 09:41:21.392: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&mode=M&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&mode=M&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&mode=M&pid=892182
07-29 09:41:25.852: DEBUG/ImageManager(13482): download from http://www.mysite.com/pic.php?hash=xyz&mode=M&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&mode=M&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 ...

仅供参考,如果我拿出支票(如果下载长度> 0)重试,我最终只会下载文件1,3,5等......

0 个答案:

没有答案