java - 在android中更快地下载给定url的图像

标签 java android image download

我有一些图像的网址,我想下载这些图像并将其设置到 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;
        }

}

等待任何能够完成相同任务但速度更快的提示或源代码。谢谢

最佳答案

您可以使用缓存。我正在使用 Square 的 Picasso 从网络加载图像并将其显示在 ImageView 中。 Picasso 有多种缓存实现,并且使用起来非常简单。

https://github.com/square/picasso

关于java - 在android中更快地下载给定url的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19955972/

相关文章:

android - 在 Android Studio 中运行我的项目时出现警告 :No JDK specified for module 'Myproject' .

c - C语言如何将图片复制到目录

javascript - 使用 require ('...' ) 在 react js 中动态导入图像不再起作用

java - 如何让 Maven 使用正确的存储库?

用于 Oracle 访问的 Java 代码在执行期间失败。我该如何修复我的代码?

java - (Triangle 类)设计一个名为 Triangle 的类,它扩展了 GeometricObject

java - 为什么 JFreeChart 中没有文本?

java - 如何根据列表副本中的位置从列表中删除值?

android - SwipeListView : View is messed up. 隐藏内容遍布列表项

java - 如何在java中调整文本大小