2013-01-12 47 views
0

嗨,所以我想通過AsyncTask抓取圖像從url鏈接,抓取圖像本身的功能工作正常。但我想要做的是將src變量傳遞給asyncTask,這似乎不適合我。退貨將是空白的。將參數傳遞到AsyncTask

這裏是代碼:

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

,我這樣稱呼它在的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); 
+0

他將url傳遞給getprofile方法 –

回答

1

AsyncTask幫助您完成異步作業。在你的代碼中,我可以看到你在調用它之後立即返回Drawable。但那一刻,你的asynctask還沒有完成,可繪製仍然是空的。

task2.execute(src); 
return profile; 

如果你想設置繪製資源時完成作業的AsyncTask,只要把你的ImageView到你的方法。它應該是:

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

使用它:

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

希望這有助於。

更新
沒有辦法直接返回從異步方法值,這裏是另一種選擇。 首先,創建一個界面,以便在完成作業時進行通知。

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

然後您的活動類應該是這樣的:

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(); 
} 
    } 
+0

我得到你來自哪裏,但如果我有多個imageview來填充? – Sam

+0

哦,我剛剛看到。但如果我想要它返回一個drawable而不是imageview。因爲我需要將drawable傳遞給另一個函數。 – Sam

+0

讓我說我需要它返回一個drawable,而我嘗試了最後的Drawable draw,但是我不能設置draw的值,因爲它是在一個封閉的括號內。我該如何解決這個問題。 – Sam

0

寫在你的活動公共成員函數返回繪製,只是調用doInBackground(功能您的AsyncTask類的)方法。

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

現在只是調用doInBackground()方法,這個功能並保存返回的結果在您的活動的一些變量。

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

編輯:的AsyncTask是你的後臺線程,而不是UI線程,所以如果你想要做的任何UI工作,那麼你將不得不runOnUiThread()方法

執行在UI線程工作
runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

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

這樣就不會因爲從網站上下載圖片調用而在UI中凍結?所以我做doInBackground內的runUIThread? – Sam

+0

不,因爲asyncTask是您的後臺線程,所以不會有任何凍結,asyncTask的目的是在後臺執行保持UI活動的任務。但是當你真的想根據長時間運行的操作的結果改變你的UI時,你應該在runOnUiThread方法中執行UI更改。 – Farooq