Saturday, September 29, 2012

execute AsyncTask with series of parameters to load image(s) from internet

The previous example describe how to "Load Bitmap from internet in background thread using AsyncTask" to load ONE image only. If you have to load 10 images, is it need to create 10 AsyncTasks? obviously NO.

It can be noticed that the parameters passed to doInBackground() method is URL... urls, a series of parameter. In order to pass data to doInBackground(), put them in execute() call of AsyncTask object.

Here is the modified code to load 5 images from internet in AsyncTask, and update ImageViews and ProgressBar on the run, in onProgressUpdate() method.

execute AsyncTask with series of parameters to load image(s) from internet


package com.example.androidcolor;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 ProgressBar loadingProgressBar;
 ImageView[] targetImage = new ImageView[5];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        targetImage[0] = (ImageView)findViewById(R.id.target0);
        targetImage[1] = (ImageView)findViewById(R.id.target1);
        targetImage[2] = (ImageView)findViewById(R.id.target2);
        targetImage[3] = (ImageView)findViewById(R.id.target3);
        targetImage[4] = (ImageView)findViewById(R.id.target4);
        
        loadingProgressBar = (ProgressBar)findViewById(R.id.loadingprogress);
        
        //Load bitmap from internet
        //As a example, I make all images load from the same source
        String onLineImage0 = "http://goo.gl/1VArP";
        String onLineImage1 = "http://goo.gl/1VArP";
        String onLineImage2 = "http://goo.gl/1VArP";
        String onLineImage3 = "http://goo.gl/1VArP";
        String onLineImage4 = "http://goo.gl/1VArP";
        URL onLineURL0, onLineURL1, onLineURL2, onLineURL3, onLineURL4;
        
  try {
   onLineURL0 = new URL(onLineImage0);
   onLineURL1 = new URL(onLineImage1);
   onLineURL2 = new URL(onLineImage2);
   onLineURL3 = new URL(onLineImage3);
   onLineURL4 = new URL(onLineImage4);
   new MyNetworkTask(5, targetImage, loadingProgressBar)
    .execute(onLineURL0, onLineURL1, onLineURL2, onLineURL3, onLineURL4);
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }
        
    }
    
    private class MyNetworkTask extends AsyncTask<URL, Integer, Void>{
     
     ImageView[] tIV;
     Bitmap[] tBM;
     ProgressBar tProgressBar;
     
     public MyNetworkTask(int numberOfImage, ImageView[] iv, ProgressBar pb){
      
      tBM = new Bitmap[numberOfImage];
      
      tIV = new ImageView[numberOfImage];
      for(int i = 0; i < numberOfImage; i++){
       tIV[i] = iv[i];
      }
      
      tProgressBar = pb;
     }

  @Override
  protected Void doInBackground(URL... urls) {

   if (urls.length > 0){
    for(int i = 0; i < urls.length; i++){
     URL networkUrl = urls[i];
     
     try {
      tBM[i] = BitmapFactory.decodeStream(
        networkUrl.openConnection().getInputStream());
     } catch (IOException e) {
      e.printStackTrace();
     }
     
     publishProgress(i);
     
     //insert dummy delay to simulate lone time operation
     try {
      Thread.sleep(500);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     
    }
   }
   return null;
  }

  @Override
  protected void onPostExecute(Void result) {
   Toast.makeText(getBaseContext(), "Finished", Toast.LENGTH_LONG).show();
  }

  @Override
  protected void onProgressUpdate(Integer... values) {

   if(values.length > 0){
    for(int i = 0; i < values.length; i++){
     tIV[values[i]].setImageBitmap(tBM[values[i]]);
     tProgressBar.setProgress(values[i]+1);
    }
   }
   
  }

    }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/loadingprogress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="5"
        android:progress="0"/>
    <ImageView
        android:id="@+id/target0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/target1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/target2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/target3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <ImageView
        android:id="@+id/target4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


Remark: Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml.

download filesDownload the files.

No comments:

Post a Comment