下载图像有时仅显示为空白图像

时间:2012-12-03 13:19:00

标签: android android-image

我从asyncTask中的网址下载图片。

我下载了100张图片,其中大约3-4张图片只是一张黑色图片,看起来下载中断或类似的东西。所以图片就像是损坏的文件或其他东西......

我真的不明白,因为我有快速稳定的互联网,而且只有3-4张图片来自100,而其他人则不一样,而不是相同的。

这是我的下载方式:

private void downloadPicture(String strURL, String id) {

    id = id.trim();

    InputStream input;
    try {

        URL url = new URL(strURL);
        input = url.openStream();
        byte[] buffer = new byte[1500];

        File DownloadFolder = new File(Environment
                .getExternalStorageDirectory().getPath() + "/myTest/");
        DownloadFolder.mkdirs();

        OutputStream output = new FileOutputStream(
                DownloadFolder.toString() + "/" + id + "samplePicture.png");
        try {
            int bytesRead = 0;
            while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
                output.write(buffer, 0, bytesRead);
            }
        } finally {
            output.close();
            buffer = null;
        }
    } catch (Exception e) {
        Log.e("Exception while grabbing image from URL", e.toString());

    }
}

我做错了什么?

2 个答案:

答案 0 :(得分:0)

首先,如果你在线程中使用这个函数downloadPicture(String strURL, String id)来并行下载图像,那么使用thread pool并保持池的大小小于5.这应该有效。

答案 1 :(得分:0)

试试这段代码。

import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.net.URLConnection;

    import org.apache.http.util.ByteArrayBuffer;

    import android.util.Log;

    public class ImageManager {

            private final String PATH = "/data/data/com.helloandroid.imagedownloader/";  //put the downloaded file here


            public void DownloadFromUrl(String imageURL, String fileName) {  //this is the downloader method
                    try {
                            URL url = new URL("http://yoursite.com/"" + imageURL); //you can write here any link
                            File file = new File(fileName);

                            long startTime = System.currentTimeMillis();
                            Log.d("ImageManager", "download begining");
                            Log.d("ImageManager", "download url:" + url);
                            Log.d("ImageManager", "downloaded file name:" + fileName);
                            /* Open a connection to that URL. */
                            URLConnection ucon = url.openConnection();

                            /*
                             * Define InputStreams to read from the URLConnection.
                             */
                            InputStream is = ucon.getInputStream();
                            BufferedInputStream bis = new BufferedInputStream(is);

                            /*
                             * Read bytes to the Buffer until there is nothing more to read(-1).
                             */
                            ByteArrayBuffer baf = new ByteArrayBuffer(50);
                            int current = 0;
                            while ((current = bis.read()) != -1) {
                                    baf.append((byte) current);
                            }

                            /* Convert the Bytes read to a String. */
                            FileOutputStream fos = new FileOutputStream(file);
                            fos.write(baf.toByteArray());
                            fos.close();
                            Log.d("ImageManager", "download ready in"
                                            + ((System.currentTimeMillis() - startTime) / 1000)
                                            + " sec");

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

            }
    }