0

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

4
  • the onPostExecute method runs on the main (UI) thread. You probably have to synchronize it yourself.
    – whlk
    Jul 18, 2011 at 18:16
  • I'm sure that I have to do that.But I don't really know how:P
    – adrian
    Jul 18, 2011 at 18:21
  • I'd go with @Override public void onPostExecute(Bitmap result) { imageView.setImageBitmap(result); synchronized(arrayImageView) { arrayImageView.add(imageView);} }
    – whlk
    Jul 19, 2011 at 7:40
  • Actually I did use a LazyAdapter to retrieve the picture from facebook.This one retrieves the pictures from facebook and stores them on the Sdcard and after that sets them to an adapter.If u need something like, I can give it to you!
    – adrian
    Jul 29, 2011 at 14:20

1 Answer 1

1

If i'm right you can do this..

 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});
progressDialog = ProgressDialog.show(context, "", "Please Wait....");//**progressDialog** an instance variable

return ImageView;

        }

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 in postExecute() and can be returned when dialog is dismissed and the UI thread resumes..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.