Use volley imageloader download picture, Onresponse will call two times, the first time bitmap is empty, set the default picture; The second time is to download the network pictures.
In the source code:
// Update the caller to let them know that they should use the default bitmap.true);
When using, before loading the picture, you need to determine whether the bitmap is empty and then loaded.
Code
/** * Download pictures from the server * Onresponse will be called two times, the first bitmap is null, the default picture is set, and the second is the network picture. * * @param Listener receive monitor */ protected void Parseimage(FinalImagelistener<bitmap> listener) {webscheduler.getinstance (). Getimageloader (). Get (Networkhelper.getrealurl ( Mimgurl),NewImageloader.imagelistener () {@Override Public void Onresponse(Imageloader.imagecontainer Imagecontainer,Booleanb) {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
/** * issues a bitmap request with the given URL if that image was not available * in the cache, and returns a BITM AP container that contains all of the data * relating to the request (as well as the default image if the requested * image is not available). *@paramRequesturl the URL of the remote image *@paramImagelistener the listener to call when the remote image is loaded *@paramMaxWidth the maximum width of the returned image. *@paramMaxHeight The maximum height of the returned image. *@paramScaleType the imageviews scaletype used to calculate the needed image size. *@returnA container object that contains all of the properties of the request, as well as * the currently available image (Default if remote is not loaded). */ PublicImagecontainerGet(String Requesturl, Imagelistener Imagelistener,intMaxWidth,intMaxHeight, ScaleType ScaleType) {//Only fulfill requests this were initiated from the main thread.Throwifnotonmainthread ();FinalString CacheKey = Getcachekey (Requesturl, MaxWidth, MaxHeight, ScaleType);//Try to look up the request in the cache of the remote images.Bitmap Cachedbitmap = Mcache.getbitmap (CacheKey);if(Cachedbitmap! =NULL) {//Return the cached bitmap.Imagecontainer container =NewImagecontainer (Cachedbitmap, Requesturl,NULL,NULL); Imagelistener.onresponse (Container,true);returnContainer }//The bitmap didn't exist in the cache, fetch it!Imagecontainer Imagecontainer =NewImagecontainer (NULL, Requesturl, CacheKey, Imagelistener);//Update the caller to let them know this 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);returnImagecontainer; }//The request is not a already in flight. Send the new request to the network and //Track it.request<bitmap> newrequest = Makeimagerequest (Requesturl, MaxWidth, MaxHeight, ScaleType, CacheKey); Mrequestqueue.add (newrequest); Minflightrequests.put (CacheKey,NewBatchedimagerequest (Newrequest, Imagecontainer));returnImagecontainer; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-volley's Imageloader download image