Java 在Android上从一系列URL下载图像并显示到多个不同的ImageView

Java 在Android上从一系列URL下载图像并显示到多个不同的ImageView,java,android,http,imageview,android-imageview,Java,Android,Http,Imageview,Android Imageview,我有这段代码从URL下载一张照片,并在Android上的ImageView中显示它 如果我有一个ArrayList或多个URL的数组要下载并显示在不同的ImageView上,我不知道如何循环。我将感谢任何关于如何进行的帮助或见解!谢谢大家! public class DisplayPhotoTask extends AsyncTask<String, Void, Bitmap> { @Override protected Bitmap doInBackground(S

我有这段代码从URL下载一张照片,并在Android上的ImageView中显示它

如果我有一个ArrayList或多个URL的数组要下载并显示在不同的ImageView上,我不知道如何循环。我将感谢任何关于如何进行的帮助或见解!谢谢大家!

public class DisplayPhotoTask extends AsyncTask<String, Void, Bitmap> {
    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;     
    }

    //sets bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {
        ImageView imageView1 = (ImageView) findViewById(R.id.imageView);
        imageView1.setImageBitmap(result);
    }

    //creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    //makes httpurlconnection and returns inputstream
    private InputStream getHttpConnection(String urlString) throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }

}
公共类DisplayPhotoTask扩展了AsyncTask{
@凌驾
受保护位图doInBackground(字符串…URL){
位图映射=空;
for(字符串url:url){
map=下载图像(url);
}
返回图;
}
//设置doInBackground返回的位图
@凌驾
受保护的void onPostExecute(位图结果){
ImageView ImageView 1=(ImageView)findViewById(R.id.ImageView);
imageView1.setImageBitmap(结果);
}
//从InputStream创建位图并返回它
私有位图下载图像(字符串url){
位图=空;
InputStream=null;
BitmapFactory.Options bmOptions=新的BitmapFactory.Options();
bmOptions.inSampleSize=1;
试一试{
流=getHttpConnection(url);
位图=BitmapFactory.decodeStream(stream,null,bmOptions);
stream.close();
}捕获(IOE1异常){
e1.printStackTrace();
}
返回位图;
}
//建立httpurlconnection并返回inputstream
私有InputStream getHttpConnection(字符串urlString)引发IOException{
InputStream=null;
URL=新URL(URL字符串);
URLConnection=url.openConnection();
试一试{
HttpURLConnection httpConnection=(HttpURLConnection)连接;
setRequestMethod(“GET”);
httpConnection.connect();
if(httpConnection.getResponseCode()==HttpURLConnection.HTTP\u确定){
stream=httpConnection.getInputStream();
}
}捕获(例外情况除外){
例如printStackTrace();
}
回流;
}
}

例如,您可以将AsyncTask的结果作为List ant编写的内容

protected Bitmap doInBackground(String... urls) {
    List<Bitmap> bitmaps = new ArrayList<Bitmap>;
    for (String url : urls) {
        bitmaps.add(downloadImage(url));
    }
    return bitmaps;     
}

protected void onPostExecute(List<Bitmap> result) {
    //...
}
受保护位图doInBackground(字符串…URL){
列表位图=新的ArrayList;
for(字符串url:url){
添加(下载图像(url));
}
返回位图;
}
受保护的void onPostExecute(列表结果){
//...
}
但我真正建议您使用Google编写的Volley库,它有非常简单和强大的API(这里是关于它和存储库的Google I/O会话)