嗨所以我试图通过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);
答案 0 :(得分:1)
AsyncTask
可帮助您完成异步作业。在您的代码中,我可以看到您在调用后立即返回Drawable
。但是那一刻,你的asynctask还没有完成,drawable仍然是null。
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();
}
}
答案 1 :(得分:0)
在您的activity中编写一个公共成员函数,它返回drawable,并在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;
}
});