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
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
imageViewis initialised with some value inpostExecute()and can be returned when dialog is dismissed and the UI thread resumes..