Android-Volley's ImageLoader downloads images

Source: Internet
Author: User

Android-Volley's ImageLoader downloads images

When you use Volley's ImageLoader to download an image, onResponse calls the image twice. The first Bitmap is empty and the default image is set. The second is the downloaded network image.
Source code:

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

Before loading an image, you must determine whether Bitmap is empty and then load it.

Code

/*** Download the image from the server * onResponse will call twice. The first Bitmap is null, and the default image is set; the second is a network image. ** @ param listener receives the 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 );}
  

Source code

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.