Search code examples
androidimageandroid-asynctask

How to load multiple image in AsyncTask


i want to load multiple images by passing a url. unlike this tutorial http://www.learn2crack.com/2014/06/android-load-image-from-internet.html in which only one image is downloaded and is viewed using img.setImageBitmap(image); My question is how can i do to send url to a method to load an image for that url and the method return an image and then i show it in an imageview. then i pass some other url to it and get image in response and i show it in some other imageview.


Solution

  • Ok assuming then you for some reason want to fetch the images one by one and in sequence, as opposed to in parallel, and assuming you have your URLs in a list, array etc, modify the tutorial example so that onPostExecute calls a method in your activity, either defined in a callback interface or on your class itself, and for each iteration pick one element from the list and start a new LoadImage job whenever the previous one is complete.

    Example (untested) code:

    public class MainActivity extends Activity {
      Button load_img;
      ImageView img;
      Bitmap bitmap;
      ProgressDialog pDialog;
      private List<String> mURLs = new LinkedList<String>();
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        load_img = (Button)findViewById(R.id.load);
        img = (ImageView)findViewById(R.id.img);
        load_img.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View arg0) {
            // TODO Auto-generated method stub
             mURLs.add("http://www.learn2crack.com/wp-content/uploads/2014/04/node-cover-720x340.png");
             mURLs.add("some-other-URL");
    
             loadNext();
          }
        });
    
        private void loadNext() {
            String url = mURLs.remove();
            if (url != null) {
                new LoadImage().execute(url);
            }
        }
      }
      private class LoadImage extends AsyncTask<String, String, Bitmap> {
        @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Loading Image ....");
                pDialog.show();
        }
           protected Bitmap doInBackground(String... args) {
             try {
                   bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
            } catch (Exception e) {
                  e.printStackTrace();
            }
          return bitmap;
           }
           protected void onPostExecute(Bitmap image) {
             if(image != null){
               img.setImageBitmap(image);
               pDialog.dismiss();
             }else{
               pDialog.dismiss();
               Toast.makeText(MainActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
             }
    
             loadNext();
           }
       }
    }
    

    In this example I set the image onto the same ImageView repeatedly which obviously makes no sense. You obviously have to keep track of which image goes onto which imageview by either sending the imageview as an argument to the task, or keep the imageviews (or their R.id.xxx ID's and call findById accordingly) in an array as well, or keep a counter, or.... There are a million ways to solve this.

    Another approach is to send the array to the AsyncTask and have doInBackground simply loop over the array with a for-loop or similar.

    Note however that a better approach might be to actually parallelize the tasks so that you make sure to utilize your available network bandwidth fully to fetch the images in parallel.