嗨,所以我试图通过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);
AsyncTask
帮助您执行异步作业。在你的代码中,我可以看到你在调用它后立即返回Drawable
。但是在那一刻,你的asynctask还没有完成,drawable仍然是空的。
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);
}
那么你的activity类应该看起来像:
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();
}
}
在你的活动中写一个公共成员函数,它返回drawable,只是在你的asyncTask类的doInBackground()方法中调用函数。
Drawable downloadImage(){
//write code here to download image so you can return any dataType
}
现在只需在doInBackground()方法中调用这个函数,并将返回的结果保存在activity的某个变量中。
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;
}
});