Android AsyncTask 下载多张图片



在我的Android应用程序中,我有一个AsyncTask可以从Web下载图片并在UI中显示它(在onPostExecute()中,我正在生成一个新的ImageView)。如何制作一个同时下载多个图像的 AsyncTask,并在下载单个图像时直接显示它们,即使其他图像尚未准备好?

这是我的代码:

public class DownloadImages extends
            AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... params) {
            Bitmap bitmap = null;
            bitmap = downloadBitmap("HERE's MY URL");

            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap result) {

            ImageView image = new ImageView(context);
            image.setImageBitmap(result);   

            ((ViewGroup) planLinearLayout).addView(image);

        }

        }
        public Bitmap downloadBitmap(String url) {
            final AndroidHttpClient client = AndroidHttpClient
                    .newInstance("Android");
            final HttpGet getRequest = new HttpGet(url);
            try {
                HttpResponse response = client.execute(getRequest);
                final int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    Log.w("ImageDownloader", "Error " + statusCode
                            + " while retrieving bitmap from " + url);
                    return null;
                }
                final HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream inputStream = null;
                    try {
                        inputStream = entity.getContent();
                        final Bitmap bitmap = BitmapFactory
                                .decodeStream(inputStream);

                        return bitmap;
                    } finally {
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        entity.consumeContent();
                    }
                }
            } catch (Exception e) {
                // Could provide a more explicit error message for IOException
                // or
                // IllegalStateException
                getRequest.abort();
                Log.w("ImageDownloader", "Error while retrieving bitmap from "
                        + url);
            } finally {
                if (client != null) {
                    client.close();
                }
            }
            return null;
        }
    }

由于您无法同时发出两个请求,因此也可以执行以下操作:

  • 首先,在您的活动中,创建您的 AsyncTask 实例并使用类似"firstImage"的参数执行它,然后在 doInBackground() 中,只需检查这是否是传递的参数的值,如果是,请下载第一个图像。下载图像后,将其保存在局部变量或任何您想要的某个位置。然后返回一个字符串,如"firstImage"。在 onPostExecute 中,只需检查结果的值并将图像传回将更新 UI 的活动。
  • 其次,使用第一个图像更新 UI 后,使用类似"secondImage"的字符串进行第二次异步调用,并重复与之前相同的过程并更新 UI - 这会将第二个图像添加到 UI。您不需要为此提供库!

我建议您使用通用图像加载器库在Android中下载图像。

它下载图像以及做一些更多好的事情,例如缓存图像和管理内存非常棘手。

更新

如果不想缓存图像,则可以使用通用图像加载器库中的Configuration禁用此功能。

图书馆链接 : https://github.com/nostra13/Android-Universal-Image-Loader

只需使用onProgressUpdate(Progress...) .将第二个泛型类型更改为 Bitmap,并在加载完图像后调用publishProgress()

我会推荐毕加索,如果你根本不想缓存图像,你可以使用skipMemoryCache()

最新更新