gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:25 25 4
gpt4 key购买 nike

我的代码是:

class MyService extends Service{
public void onCreate(){
new ImageLoader(mRequestQueue, new VolleyLruCache(cacheSize))
.get(url, new ImageListener(){..});
}
}

我希望运行良好,但它抛出 IllegalStateException 异常。所以,打开 volley 的完整源代码,找到这个。

[ImageLoader.java]

    public ImageContainer get(String requestUrl, ImageListener imageListener,
int maxWidth, int maxHeight) {
// only fulfill requests that were initiated from the main thread.
throwIfNotOnMainThread(); //<- why???

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<Bitmap>() {
@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;
}

我不明白 throwIfNotOnMainThread()..为什么 volley imageLoader 必须从主线程调用?

谢谢。

最佳答案

这是为了保证响应回调发生在主 UI 线程上,假设回调想要更新 UI。

ImageListener 的默认实现由 Volley 库提供更新 UI,因此必须从主线程使用它。

   /**
* The default implementation of ImageListener which handles basic functionality
* of showing a default image until the network response is received, at which point
* it will switch to either the actual image or the error image.
* @param imageView The imageView that the listener is associated with.
* @param defaultImageResId Default image resource ID to use, or 0 if it doesn't exist.
* @param errorImageResId Error image resource ID to use, or 0 if it doesn't exist.
*/
public static ImageListener getImageListener(final ImageView view,
final int defaultImageResId, final int errorImageResId) {
return new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (errorImageResId != 0) {
view.setImageResource(errorImageResId);
}
}
@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
if (response.getBitmap() != null) {
view.setImageBitmap(response.getBitmap());
} else if (defaultImageResId != 0) {
view.setImageResource(defaultImageResId);
}
}
};
}

关于Android:Volley: 为什么 volley imageLoader 必须从主线程调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23512736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com