帮助管理 AsynTask 线程
我在 android 中有一个应用程序,我正在尝试使用 AsyncTask
线程从互联网上检索一些图片。 问题是,我在循环中检索这些图片,因此对于每个索引,我启动一个新的 AsyncTask 线程。每个线程返回一张图片(imageView)。
这是我的问题:
启动的线程异步返回图片,因此当我尝试访问存储这些图像的列表时,我得到ArrayIndexOutofBounds
。
这是我的代码:
for(int i=0;i<friends.size();i++)
{
getUserPic(friends.get(i).getId());
}
这是 getUserPic
方法:
public void getUserPic(String userID){
imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
new Task().execute(new String[] {imageURL});
}
这是线程:
private class Task extends AsyncTask<String, Void, Bitmap>{
@Override
public Bitmap doInBackground(String...arg0){
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(arg0[0]);
try{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
profilePicBitmap=BitmapFactory.decodeStream(content);
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return profilePicBitmap;
}
@Override
public void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
arrayImageView.add(imageView);
}
}
所以我想做的是:
List<ImageView> arrayImageView=new ArrayList<ImageView>();
for(i....)
{
arrayImageView[i]=getUserPic(...);
}
public ImageView getUserPic(String userID){
imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
new Task().execute(new String[] {imageURL});
return ImageView;
}
我的意思是 AsyncTask 线程返回的每个 ImageView
也可以通过 getUserPic 方法返回。这样我就可以更轻松地跟踪它们。
问题:如何使 AsynTask
返回的图片也成为 getUserPic()
返回的变量。
我希望你明白我的意思。谢谢
I have an app in android and I'm trying to retrieve some pictures from the internet using an AsyncTask
thread.
The thing is that I'm retrieving this pictures in a loop
so for each index I'm launching a new AsyncTask
thread.Each thread returnes a picture(imageView).
And here is my problem:
The threads launched return the pictures asynchorounsly so when I'm trying to acces the List that stores these images I get ArrayIndexOutofBounds
.
This is my code:
for(int i=0;i<friends.size();i++)
{
getUserPic(friends.get(i).getId());
}
This is the getUserPic
method:
public void getUserPic(String userID){
imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
new Task().execute(new String[] {imageURL});
}
And here is the thread:
private class Task extends AsyncTask<String, Void, Bitmap>{
@Override
public Bitmap doInBackground(String...arg0){
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(arg0[0]);
try{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
profilePicBitmap=BitmapFactory.decodeStream(content);
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return profilePicBitmap;
}
@Override
public void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
arrayImageView.add(imageView);
}
}
So what I wanna do is something like:
List<ImageView> arrayImageView=new ArrayList<ImageView>();
for(i....)
{
arrayImageView[i]=getUserPic(...);
}
public ImageView getUserPic(String userID){
imageURL = "http://graph.facebook.com/"+userID+"/picture?type=small";
new Task().execute(new String[] {imageURL});
return ImageView;
}
I mean each ImageView
return by the AsyncTask thread to be returned also by the getUserPic
method.So would be more easily for me to keep track of them.
Question:How do I proceed so the pictured returned by AsynTask
to be also the variable that getUserPic()
returns.
I hope u get what I mean.Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我是对的,你可以这样做..
并在 postExecute 中关闭此对话框..我建议这样做,因为此时 UI 线程将被阻止,你可以确保你的
imageView
已用一些初始化postExecute()
中的值,并且可以在对话框关闭且 UI 线程恢复时返回。If i'm right you can do this..
and in postExecute dismiss this dialog.. I suggest this because at this moment the UI thread will be blocked and u can make sure your
imageView
is initialised with some value inpostExecute()
and can be returned when dialog is dismissed and the UI thread resumes..