0

嗨,我正在尝试通过 AsyncTask 从 url 链接中获取图像,获取图像本身的功能可以正常工作。但我想做的是将 src 变量传递给 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);
4

2 回答 2

1

AsyncTask帮助您完成异步工作。在您的代码中,我可以看到您Drawable 在调用它后立即返回。但是在那一刻,您的 asynctask 尚未完成,并且 drawable 仍然为空。

task2.execute(src);
return profile;

如果您想在异步任务中完成作业时设置可绘制资源,只需将您的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();
}
  }
于 2013-01-12T09:57:37.087 回答
0

在您的活动中编写一个公共成员函数,该函数返回可绘制对象,只需在 asyncTask 类的 doInBackground() 方法中调用该函数。

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;
           }
       });
于 2013-01-12T10:05:53.693 回答