使用android< = 2.2下载图像

时间:2012-11-19 09:45:30

标签: android image

我从互联网下载图片时遇到问题。 问题是图像互相下载。 程序在android< = 2.2上运行时出现问题。 下载约50张图像,分辨率为320x200

代码片段:

public class DownloadOfferImages extends AsyncTask<List<ImageOfferData>, Bitmap, Void> {

    private InputStream inputStream;
    private BufferedInputStream buffer;

    @Override
    protected Void doInBackground(List<ImageOfferData>... offer) {
        for(int i = 0 ; !isInteruppted() && i < offer[0].size() ; i++) {
            if(offer[0].get(i).getImage() == null)
                downloadImage(offer[0].get(i));
        }
        return null;
    }

    private void downloadImage(ImageOfferData offerData) {
        try {
            download(offerData);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                closeStreams();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void download(ImageOfferData offerData) throws MalformedURLException, OutOfMemoryError, IOException {
        URL imageUrl = new URL(offerData.getImageURL());
        URLConnection connection = imageUrl.openConnection();
        inputStream = connection.getInputStream();
        buffer = new BufferedInputStream(inputStream);
        Bitmap image = BitmapFactory.decodeStream(buffer);
        buffer.close();
        offerData.setImage(image);
        publishProgress(image);
    }

    /*
     * [rest of code]
     * 
     * 
     * */

}

1 个答案:

答案 0 :(得分:1)

我强烈推荐精彩的Async Image Loader库:

自述文件中包含的示例。在github上找到它:

https://github.com/nostra13/Android-Universal-Image-Loader