0

我从互联网下载图像时遇到问题。问题是图像正在下载。当程序在 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]
     * 
     * 
     * */

}
4

1 回答 1

1

我强烈推荐美妙的 Async Image Loader 库:

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

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

于 2012-11-19T10:03:45.057 回答