帮助管理 AsynTask 线程

发布于 2024-11-24 19:56:48 字数 2158 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空气里的味道 2024-12-01 19:56:48

如果我是对的,你可以这样做..

 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;

        }

并在 postExecute 中关闭此对话框..我建议这样做,因为此时 UI 线程将被阻止,你可以确保你的 imageView 已用一些初始化postExecute() 中的值,并且可以在对话框关闭且 UI 线程恢复时返回。

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文