Sam
Sam

Reputation: 33

Passing parameters into AsyncTask

Hi so i'm trying to grab a image from a url link via AsyncTask, the function to grab the image itself work fine. but what i trying to do is to pass the src variable into a asyncTask which seems to be not working for me. the return will be blank.

here is the code:

 private AsyncTask<String, Void, Drawable> task2;
 Drawable profile;
 public Drawable getProfile(String src){        
    task2 = new AsyncTask<String, Void, Drawable>() {           
        ProgressDialog dialog2;
        InputStream is;
        Drawable d;
        @Override 
        protected void onPreExecute(){
            dialog2 = new ProgressDialog(Thoughts.this, ProgressDialog.STYLE_SPINNER);
            dialog2.setMessage("Loading Data...");          
            dialog2.setCancelable(false);
            dialog2.setCanceledOnTouchOutside(false);   
            dialog2.show();
        }
        @Override
        protected Drawable doInBackground(String... src) {
                try
                {
                    is = (InputStream) new URL(src[0]).getContent();
                    d = Drawable.createFromStream(is, "src name");
                    return d;
                }catch (Exception e) {
                    e.toString();
                    return null;
                }
        }
        @Override
        protected void onPostExecute(Drawable result2) {
            profile = result2; 
            dialog2.dismiss();
        }
    };
    task2.execute(src);
    return profile;
}

and i call it like this at the onCreate();

Drawable p4 = getProfile("http://..../xyz.jpg");
Drawable p5 = getProfile("http://..../xyz.jpg");

ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
ImageView thoughtsProfilePic1 =(ImageView) findViewById(R.id.ivProfilePicData1);

thoughtsProfilePic.setImageDrawable(p4);
thoughtsProfilePic1.setImageDrawable(p5);

Upvotes: 0

Views: 891

Answers (2)

ductran
ductran

Reputation: 10193

AsyncTask help you do an asynchronous job. In your code, I can see you return Drawable right after calling it. But at that moment, the your asynctask hasn't completed yet and drawable still null.

task2.execute(src);
return profile;

If you want set drawable resource when complete job in asynctask, just put your ImageView into your method. It should be:

  public void getProfile(String src, final ImageView v){           

         @Override
    protected void onPostExecute(Drawable result2) {

        // set drawable for ImageView when complete.
        v.setImageDrawable(result2);
        dialog2.dismiss();
    }
    task2.excute(src);
    //do not need return anything.
  } 

Use it:

 ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
 getProfile("http://..../xyz.jpg", thoughtsProfilePic );

Hope this help.

Update:
There is no way to return value from asynchronous method directly, here is another choice. First, create an interface to notify when complete job.

 public interface INotifyComplete{  
      public void onResult(Drawable result);  
 } 

Then your activity class should look like:

 public class YourActivity extends Activity implement INotifyComplete{
 private Drawable res1;
 private Drawable res2;
 public void onResult(Drawable result){
    if(result == res1){
        // do something with resource 1
        ImageView thoughtsProfilePic =(ImageView) findViewById(R.id.ivProfilePicData);
        thoughtsProfilePic.setImageDrawable(result);
    }
    if(result == res2){
        // do something with resource 2
    }
  }

 void someMethod(){
// you can use this way to call
    getProfile("http://..../xyz.jpg", res1, YourActivity.this);
    //or this
    getProfile("http://..../xyz.jpg", res2, new INotifyComplete(){
        public void onResult(Drawable result){
            // result is res2, so don't need to check
        }
    });
 }
 public void getProfile(String src, final Drawable res, final INotifyComplete notify){
   //don't need store asynctask instance
    AsyncTask<String, Void, Drawable>  task2 = new AsyncTask<String, Void, Drawable>(){

        // do something ...

        protected void onPostExecute(Drawable result2) {             
        dialog2.dismiss();
        // you will set the value to your drawable then notify it when complete job
        res = result2;
        notify.onResult(res);
    }
    }
    task2.excute();
}
  }

Upvotes: 1

Farooq
Farooq

Reputation: 426

write a public member function in your activity which returns drawable and just call the function in doInBackground() method of your asyncTask class.

Drawable downloadImage(){
//write code here to download image so you can return any dataType
}

now just call this function in doInBackground() method and save returned result in some variable of your activity.

like

void doInBackground(){
    drawableVariable = downloadImage();    
}

Edit: asyncTask is your background thread and is not UI thread so if you want to do any UI work then you will have to perform that work in UI thread by runOnUiThread() method

runOnUiThread(new Runnable() {
           @Override
           public void run() {

               /* update your UI here for example what you are doing something */;
               profile = result2;
           }
       });

Upvotes: 0

Related Questions