android volley 下载图片大小,Android - Volley的ImageLoader下载图片

Android - Volley的ImageLoader下载图片

浏览次数:

发布时间:2015-08-09 07:00

使用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 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 newRequest = makeImageRequest(requestUrl, maxWidth, maxHeight, scaleType,

cacheKey);

mRequestQueue.add(newRequest);

mInFlightRequests.put(cacheKey,

new BatchedImageRequest(newRequest, imageContainer));

return imageContainer;

}

相关报道:

在我们平时的网站开发过程中,并不是所有的网页都是后台查询出一个DataTable或者List然后绑定到DataGrid上。我们可能还会遇到前台需要后台的某一个变量,或者前 更多

在ueditor文件目录里面找到php/config.json文件进行修改 我的目的是想将上传目录修改到网站根目录下面的uploadfile目录,,我是这样修改的,直接看代码就明白...上面标志出来的就是我修改过的代码 /* 前后端通信相关的配置,注释只允许使用多行方式 */{/* 上传图 更多

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值