android volley 线程,Android:Volley:为什么必须从主线程调用volley imageLoader?

我的代码是:

class MyService extends Service{

public void onCreate(){

new ImageLoader(mRequestQueue, new VolleyLruCache(cacheSize))

.get(url, new ImageListener(){..});

}

}

我希望运行良好,但它会抛出IllegalStateException Exception.

所以,打开凌空全部源,找到这个.

[ImageLoader.java]

public ImageContainer get(String requestUrl, ImageListener imageListener,

int maxWidth, int maxHeight) {

// only fulfill requests that were initiated from the main thread.

throwIfNotOnMainThread(); //

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

// 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 =

new ImageRequest(requestUrl, new Listener() {

@Override

public void onResponse(Bitmap response) {

onGetImageSuccess(cacheKey, response);

}

}, maxWidth, maxHeight,

Config.RGB_565, new ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

onGetImageError(cacheKey, error);

}

});

mRequestQueue.add(newRequest);

mInFlightRequests.put(cacheKey,

new BatchedImageRequest(newRequest, imageContainer));

return imageContainer;

}

我不明白th​​rowIfNotOnMainThread()..

为什么必须从主线程调用volley imageLoader?

谢谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值