Android - Volley的ImageLoader下载图片

使用Volley的ImageLoader下载图片时, onResponse会调用两次, 第一次Bitmap是空, 设置默认图片; 第二次是下载的网络图片.

源码中:

 // Update the caller to let them know that they should use the default bitmap.
imageListener.onResponse(imageContainer, true);

使用时, 在加载图片之前, 需要判断Bitmap是否为空, 再加载.



代码

    /**
     * 从服务器下载图片
     * onResponse会调用两次, 第一次Bitmap是null, 设置默认图片; 第二次是网络图片.
     *
     * @param listener 接收监听
     */
    protected void parseImage(final ImageListener<Bitmap> listener) {
        WebScheduler.getInstance().getImageLoader().get(NetworkHelper.getRealUrl(mImgUrl),
                new ImageLoader.ImageListener() {
                    @Override
                    public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
                        mImgBitmap = imageContainer.getBitmap();
                        if (mImgBitmap != null)
                            listener.onResponse(mImgBitmap);
                    }

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        ToastHelper.getInstance().showToast(R.string.network_error);
                    }
                }, 0, 0
        );
    }


源码

    /**
     * Issues a bitmap request with the given URL if that image is not available
     * in the cache, and returns a bitmap container that contains all of the data
     * relating to the request (as well as the default image if the requested
     * image is not available).
     * @param requestUrl The url of the remote image
     * @param imageListener The listener to call when the remote image is loaded
     * @param maxWidth The maximum width of the returned image.
     * @param maxHeight The maximum height of the returned image.
     * @param scaleType The ImageViews ScaleType used to calculate the needed image size.
     * @return A container object that contains all of the properties of the request, as well as
     *     the currently available image (default if remote is not loaded).
     */
    public ImageContainer get(String requestUrl, ImageListener imageListener,
            int maxWidth, int maxHeight, ScaleType scaleType) {

        // only fulfill requests that were initiated from the main thread.
        throwIfNotOnMainThread();

        final String cacheKey = getCacheKey(requestUrl, maxWidth, maxHeight, scaleType);

        // Try to look up the request in the cache of remote images.
        Bitmap cachedBitmap = mCache.getBitmap(cacheKey);
        if (cachedBitmap != null) {
            // Return the cached bitmap.
            ImageContainer container = new ImageContainer(cachedBitmap, requestUrl, null, null);
            imageListener.onResponse(container, true);
            return container;
        }

        // The bitmap did not exist in the cache, fetch it!
        ImageContainer imageContainer =
                new ImageContainer(null, requestUrl, cacheKey, imageListener);

        // Update the caller to let them know that they should use the default bitmap.
        imageListener.onResponse(imageContainer, true);

        // Check to see if a request is already in-flight.
        BatchedImageRequest request = mInFlightRequests.get(cacheKey);
        if (request != null) {
            // If it is, add this request to the list of listeners.
            request.addContainer(imageContainer);
            return imageContainer;
        }

        // The request is not already in flight. Send the new request to the network and
        // track it.
        Request<Bitmap> newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType,
                cacheKey);

        mRequestQueue.add(newRequest);
        mInFlightRequests.put(cacheKey,
                new BatchedImageRequest(newRequest, imageContainer));
        return imageContainer;
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 12:01:06

Android - Volley的ImageLoader下载图片的相关文章

Android从网络上下载图片实现

1.背景介绍 网络上图片的请求,是我们最常见的网络请求之一,不亚于对json/xml数据的请求.一般要展示给用户看的,都不会是纯粹的文字,往往都是图文信息.而在移动互联网时代,图文又往往需要最新的资讯,数据都是从网络上获取. 像我们都在使用的微信,它的朋友圈中就好多图文信息:使用的新浪微博,用户的图标也是图片信息,等等诸如此类.由此可见,对于图片的请求处理,非常重要,我们做开发的应该掌握.今天介绍一下笔者在开发Android项目过程中使用过的一些代码. 2.思路分析 (1)取得与服务器的连接 (

Android Volley.jar包下载

Android Volley下载 Android Volley是Android平台上很好用的第三方开源网络通信框架.使用简答,功能强大. 现在我把Android Volley的库jar包Volley.jar放出来供大家下载使用,下载连接地址:http://download.csdn.net/detail/zhangphil/9053413 使用方法:下载Volley.jar后,直接放到Eclipse工程的libs目录下,刷新,然后就可以调用Android Volley了. 附: Android

Android从服务端下载图片问题

============问题描述============ 下图是我的Myeclipse项目的webroot文件夹, 我想下载这个文件夹中的图片,可是我发现出现了诡异的错误.除了test01_upload_1.jpg以外,其它的图片都显示FileNotFoundException. 下面是我的测试代码和错误信息. package com.example.imagedownload; import java.io.IOException; import java.net.MalformedURLEx

Android Volley (1)

1. volley的简单介绍 Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository. Volley 是一个HTTP库.它使得android app的网络数据传输更简单,更快. Volley的设计目标就是非常适合去进行数据量不大,但通信频繁的网络

Android Volley 库的使用

本文内容 什么是 Volley 库 Volley 能做什么 Volley 架构 演示 Volley 库的使用 参考资料 Android 关于网络操作一般都会介绍 HttpClient 以及 HttpConnection 这两个包.前者是 Apache 开源库,后者是 Android 自带 API.企业级应用,一般都会选择使用已经封装好的 http 框架.比较流行有 Volley.android-async-http.retrofit.okhttp.androidquery.AndroidAsyn

android Volley 框架详解

开发android应用很多时候都要涉及网络操作,Android SDK中提供了HttpClient 和 HttpUrlConnection两种方式用来处理网络操作,但当应用比较复杂的时候需要我们编写大量的代码处理很多东西:图像缓存,请求的调度等等: 而Volley框架就是为解决这些而生的,它与2013年Google I/O大会上被提出:使得Android应用网络操作更方便更快捷:抽象了底层Http Client等实现的细节,让开发者更专注与产生RESTful Request.另外,Volley在

Android Volley框架的使用(三)

Image Request 为了更方便的使用Volley中的图片请求,我们同样先在VolleyController类中封装一个ImageLoader package com.javen.volley; import android.content.Context; import android.text.TextUtils; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.

Android 利用Service实现下载网络图片至sdk卡

1 package com.example.myapp5; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 /** 9 * Android 利用service实现下载图片功能 10 * @author shaobn 11 * @date

Android Volley框架的使用(二)

使用请求队列RequestQueue Volley中的Request都需要添加到RequestQueue中才能执行,所以首先需要创建一个RequestQueue RequestQueue = Volley.newRequestQueue(mContext); 通常情况在一个应用中需要统一管理一个请求队列,所以采用单例模式(注意:这不是必须的),创建一个类并在这个类中初始化RequestQueue等核心对象,以及实现一些我们所需的方法: 代码如下: package com.javen.volley