2

我有一些图片的 url,我想下载这些并在 android 中设置为我的 imageView。我正在使用以下代码。它只是在执行任务。但是这个过程在我看来太慢了。我知道这取决于我设备中的互联网连接速度,但是还有什么更快的方法来加快加载图像的速度吗?

import java.io.InputStream;
import java.lang.ref.WeakReference;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;

class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;

        public ImageDownloaderTask(ImageView imageView) {
                imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) {
                // params comes from the execute() call: params[0] is the url.
                return downloadBitmap(params[0]);
        }

        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
                if (isCancelled()) {
                        bitmap = null;
                }

                if (imageViewReference != null) {
                        ImageView imageView = imageViewReference.get();
                        if (imageView != null) {

                                if (bitmap != null) {
                                        imageView.setImageBitmap(bitmap);
                                } else {
                                        imageView.setImageDrawable(imageView.getContext().getResources()
                                                        .getDrawable(R.drawable.imgloading));
                                }
                        }

                }
        }

        static 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;
        }

}

等待任何可以执行相同任务但速度更快的提示或源代码。谢谢

4

2 回答 2

2

你可以使用缓存。我正在使用 Square 的 Picasso 从网络加载图像并在 ImageView 中显示它们。Picasso 有几个缓存实现,使用起来非常简单。

https://github.com/square/picasso

于 2013-11-13T14:32:41.327 回答
1

也许这不是最好和最干净的方法,但在我看来,根据我的经验,它是禁食。

我喜欢使用 WebView 来加载图像,当你有很多图像时效果很好,而且很简单:

image.setBackgroundColor(0);
image.loadDataWithBaseURL("", "<img src='YOUR URL HERE'/>", 
                          "text/html", "UTF-8", "");

其中图像是 WebView:

WebView image = (WebView) findViewById(R.id.imagewebview);
于 2014-02-12T14:12:34.573 回答