简体   繁体   中英

Downloading and displaying images from an array of URLs to multiple different ImageViews on Android

I have this code to download a photo from a URL and display it in an ImageView on Android.

I am not sure how to loop this if I had a ArrayList or Array of multiple Urls to download and display on different ImageViews. I would appreciate any help or insight on how to proceed! Thank you!

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;
    }

}

You can for example make a result of AsyncTask as List ant write something like this

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) {
    //...
}

But what I really would recommend you is to use Volley library written by Google, it has really easy and powerful API (here is Google I/O session about it https://developers.google.com/live/shows/474338138 and repository https://android.googlesource.com/platform/frameworks/volley/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM