Android:Volley: Why volley imageLoader must be invoked from the main thread?

JuL

My code is:

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

I expect working well, but it throw IllegalStateException Exception. So, open the volley full source, find this.

[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;
}

I don't understand throwIfNotOnMainThread().. Why volley imageLoader must be invoked from the main thread?

Thanks.

Manish Mulimani

It is to guarantee that the response callback happens on the main UI thread assuming that the callback would want to update the UI.

The default implementation of ImageListener provided by Volley library updates the UI and hence it has to be used from main thread.

   /**
     * 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);
                }
            }
        };
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android:Volley: Why volley imageLoader must be invoked from the main thread?

From Dev

Why ImageLoader cannot be found in volley in Android

From Dev

Android, Volley Request, the response is blocking main thread

From Dev

Android, Volley Request, the response is blocking main thread

From Dev

Android Volley imageloader with % progress while downloading images

From Dev

Android Volley error in getInstance(this) when adding ImageLoader

From Dev

Android Volley: Combine JsonObjectRequest & ImageLoader, NullPointerException

From Dev

How to use ImageLoader of Volley Library?

From Dev

Why Android-Volley is faster

From Dev

Volley cache images with imageloader before done serializing

From Dev

Why Android Volley Response String is so short

From Dev

Why does `std::promise::set_value` throw an error when invoked from the main thread?

From Dev

Android: Changing HTTP library from Volley to OkHttp

From Dev

Android set up Volley to use from cache

From Dev

Parsing json from url with Volley in a Fragment in Android

From Dev

Android Volley return value from response

From Dev

Fetch Image from server through volley in android

From Dev

Android error: recreate() must be called from main thread

From Dev

Why eclipse dont find projects in import volley (android) directory from git?

From Dev

Update UI thread after network request in Volley Android library

From Dev

Can i use all volley network operations in a separate thread in android?

From Dev

Update UI thread after network request in Volley Android library

From Dev

why to use volley or picasso

From Dev

Why there is a mWaitingRequests in RequestQueue in Volley

From Dev

Volley, How many times the onResponse in ImageLoader.ImageListener called

From Dev

Volley ImageLoader gives 403 response for some https url

From Dev

Why does the main thread's delegate get invoked first?

From Dev

Volley not return response to main activity

From Dev

Volley cant get error from server but shows me volley error listener why?

Related Related

  1. 1

    Android:Volley: Why volley imageLoader must be invoked from the main thread?

  2. 2

    Why ImageLoader cannot be found in volley in Android

  3. 3

    Android, Volley Request, the response is blocking main thread

  4. 4

    Android, Volley Request, the response is blocking main thread

  5. 5

    Android Volley imageloader with % progress while downloading images

  6. 6

    Android Volley error in getInstance(this) when adding ImageLoader

  7. 7

    Android Volley: Combine JsonObjectRequest & ImageLoader, NullPointerException

  8. 8

    How to use ImageLoader of Volley Library?

  9. 9

    Why Android-Volley is faster

  10. 10

    Volley cache images with imageloader before done serializing

  11. 11

    Why Android Volley Response String is so short

  12. 12

    Why does `std::promise::set_value` throw an error when invoked from the main thread?

  13. 13

    Android: Changing HTTP library from Volley to OkHttp

  14. 14

    Android set up Volley to use from cache

  15. 15

    Parsing json from url with Volley in a Fragment in Android

  16. 16

    Android Volley return value from response

  17. 17

    Fetch Image from server through volley in android

  18. 18

    Android error: recreate() must be called from main thread

  19. 19

    Why eclipse dont find projects in import volley (android) directory from git?

  20. 20

    Update UI thread after network request in Volley Android library

  21. 21

    Can i use all volley network operations in a separate thread in android?

  22. 22

    Update UI thread after network request in Volley Android library

  23. 23

    why to use volley or picasso

  24. 24

    Why there is a mWaitingRequests in RequestQueue in Volley

  25. 25

    Volley, How many times the onResponse in ImageLoader.ImageListener called

  26. 26

    Volley ImageLoader gives 403 response for some https url

  27. 27

    Why does the main thread's delegate get invoked first?

  28. 28

    Volley not return response to main activity

  29. 29

    Volley cant get error from server but shows me volley error listener why?

HotTag

Archive