Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中下载给定url的图像速度更快_Java_Android_Image_Download - Fatal编程技术网

Java 在android中下载给定url的图像速度更快

Java 在android中下载给定url的图像速度更快,java,android,image,download,Java,Android,Image,Download,我有一些图片的url,我想下载这些图片并在android中设置我的imageView。我正在使用下面的代码。它只是在执行任务。但这个过程在我看来太慢了。我知道这取决于我设备中的互联网连接速度,但还有没有更快的方法来加速加载图像 import java.io.InputStream; import java.lang.ref.WeakReference; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse;

我有一些图片的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;
        }

}
import java.io.InputStream;
导入java.lang.ref.WeakReference;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.HttpStatus;
导入org.apache.http.client.methods.HttpGet;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.net.http.AndroidHttpClient;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.ImageView;
类ImageDownloaderTask扩展异步任务{
私有最终WeakReference imageViewReference;
公共图像下载任务(图像视图图像视图){
imageViewReference=新的WeakReference(imageView);
}
@凌驾
//实际下载方法,在任务线程中运行
受保护位图doInBackground(字符串…参数){
//params来自execute()调用:params[0]是url。
返回下载位图(参数[0]);
}
@凌驾
//下载图像后,将其与imageView关联
受保护的void onPostExecute(位图){
如果(isCancelled()){
位图=空;
}
if(imageViewReference!=null){
ImageView=imageViewReference.get();
如果(imageView!=null){
if(位图!=null){
设置图像位图(位图);
}否则{
imageView.setImageDrawable(imageView.getContext().getResources())
.getDrawable(R.drawable.imgloading));
}
}
}
}
静态位图下载位图(字符串url){
最终AndroidHttpClient客户端=AndroidHttpClient.newInstance(“Android”);
最终HttpGet getRequest=新HttpGet(url);
试一试{
HttpResponse response=client.execute(getRequest);
final int statusCode=response.getStatusLine().getStatusCode();
if(statusCode!=HttpStatus.SC\u OK){
Log.w(“ImageDownloader”,“Error”+状态码
+从“+url”检索位图时);
返回null;
}
最终HttpEntity=response.getEntity();
如果(实体!=null){
InputStream InputStream=null;
试一试{
inputStream=entity.getContent();
最终位图位图=位图工厂.decodeStream(inputStream);
返回位图;
}最后{
如果(inputStream!=null){
inputStream.close();
}
entity.consumercontent();
}
}
}捕获(例外e){
//可以为IOException或提供更明确的错误消息
//非法国家例外
getRequest.abort();
Log.w(“ImageDownloader”,“从“+url”检索位图时出错);
}最后{
如果(客户端!=null){
client.close();
}
}
返回null;
}
}

等待任何提示或源代码来完成相同的任务,但速度更快。Thanx可以使用缓存。我正在使用Square的毕加索从web加载图像并在ImageView中显示它们。毕加索有几种缓存实现,使用起来非常简单


也许这不是最好、最干净的方法,但在我看来,从我的经验来看,这是最快的方法

我喜欢使用WebView加载图像,当您有很多图像时效果非常好,非常简单:

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

Stackoverflow和web上已经有很多线程可用。无论如何,您只需要实现延迟加载(即异步图像加载)逻辑即可在ListView中加载图像。检查并尝试:
WebView image = (WebView) findViewById(R.id.imagewebview);