Android-universal-image-loader image download tips from source code analysis

Source: Internet
Author: User
Tags getstream

In particular, on the phone to consider the impact of the network on the image download, the common situation is that in the 2G network, 3G network needs a different download strategy, that is, in the slow network and fast network download need to consider different strategies. A common strategy is the way Android clients and services retransmitting together to optimize images for slow networks (to keep the quality of the pictures low and guaranteed to download), but this is not the case in this article. In this article, we mainly discuss the download strategy for the image quality of the server that cannot be changed (image size XX KB), Android-universal-image-loader.

Need to specifically consider network conditions are: fast, slow, no network permissions. For these three cases, three strategies were defined in UIL. Let's start with the code. In the "From the Code analysis Android-universal-image-loader picture loading, display process" We analyzed the picture download is from the Loadanddisplayimagetask.decodeimage (...) , where Getdownloader () is called inside the function, and the inputstream implementation of the image is downloaded and parsed in the implementation class (Baseimagedecoder) of the Imagedecoder interface. Follow up to Getdownloader () to see.

 private   Imagedownloader Getdownloader () {        Imagedownloader D;  if   (Engine.isnetworkdenied ()) {D = Networkdenieddownloader;  else  if   (Engine.isslownetwork ()) {D  = SLOWNETWORKD        Ownloader;  else   {D  = Downloader;     return   D; }

What exactly is Networkdenieddownloader, Slownetworkdownloader, and downloader? In the Loadanddisplayimagetask constructor we see that they are actually derived from the corresponding networkdenieddownloader in the Imageloaderconfiguration class, Slownetworkdownloader, Downloader. In Imageloaderconfiguration's constructor, we find that downloader originates from Imageloaderconfiguration.builder, After analysis, it is found to be a Baseimagedownloader object (finally in Defaultconfigurationfactory.createimagedownloade (...). is initialized). Go back to the constructor of the Imageloaderconfiguration class (shown below)

PrivateImageloaderconfiguration (FinalBuilder Builder) {Resources=builder.context.getResources (); Maximagewidthformemorycache=Builder.maximagewidthformemorycache; Maximageheightformemorycache=Builder.maximageheightformemorycache; Maximagewidthfordiskcache=Builder.maximagewidthfordiskcache; Maximageheightfordiskcache=Builder.maximageheightfordiskcache; Processorfordiskcache=Builder.processorfordiskcache; Taskexecutor=Builder.taskexecutor; Taskexecutorforcachedimages=builder.taskexecutorforcachedimages; ThreadPoolSize=builder.threadpoolsize; ThreadPriority=builder.threadpriority; Tasksprocessingtype=Builder.tasksprocessingtype; DiskCache=Builder.diskcache; MemoryCache=Builder.memorycache; Defaultdisplayimageoptions=builder.defaultdisplayimageoptions; Downloader 
=
 builder.downloader;         = Builder.decoder;         = builder.customexecutor;         = builder.customexecutorforcachedimages;        
= new Networkdeniedimagedownloader (downloader); Slownetworkdownloader = new
Slownetworkimagedownloader (downloader);        L.writedebuglogs (builder.writelogs);    }

We find that Networkdenieddownloader and slownetworkdownloader all rely on downloader objects, and guess that these two classes should be a wrapper for Baseimagedownloader. Below we put out Networkdeniedimagedownloader, Slownetworkimagedownloader code (they are in the Com.nostra13.universalimageloader.core.ImageLoaderConfiguration class)

/*** Decorator. Prevents downloads from network (throws {@linkillegalstateexception Exception}).     <br/> * In the most cases this downloader shouldn ' t is used directly. *     * @authorSergey Tarasevich (nostra13[at]gmail[dot]com) *@since1.8.0*/    Private Static classNetworkdeniedimagedownloaderImplementsImagedownloader {Private FinalImagedownloader Wrappeddownloader;  PublicNetworkdeniedimagedownloader (Imagedownloader wrappeddownloader) { This. Wrappeddownloader =Wrappeddownloader; } @Override PublicInputStream GetStream (String Imageuri, Object Extra)throwsIOException {Switch(Scheme.ofuri (Imageuri)) { CaseHTTP: CaseHTTPS:Throw Newillegalstateexception (); default:                    returnWrappeddownloader.getstream (Imageuri, extra); }        }    }    /*** Decorator. Handles <a href= "http://code.google.com/p/android/issues/detail?id=6066">this problem</a> on slow networks * using {@linkCom.nostra13.universalimageloader.core.assist.FlushedInputStream}. * *@authorSergey Tarasevich (nostra13[at]gmail[dot]com) *@since1.8.1*/    Private Static classSlownetworkimagedownloaderImplementsImagedownloader {Private FinalImagedownloader Wrappeddownloader;  PublicSlownetworkimagedownloader (Imagedownloader wrappeddownloader) { This. Wrappeddownloader =Wrappeddownloader; } @Override PublicInputStream GetStream (String Imageuri, Object Extra)throwsIOException {inputstream ImageStream=Wrappeddownloader.getstream (Imageuri, extra); Switch(Scheme.ofuri (Imageuri)) { CaseHTTP: CaseHTTPS:return NewFlushedinputstream (ImageStream); default:                    returnImageStream; }        }    }

First see the Networkdeniedimagedownloader class, in this class because the corresponding is no network access rights (Android. permission. INTERNET), in which case HTTP and HTTPS are naturally not available, and other conditions, such as obtaining a picture from a local resource, are still possible. What is the Networkdeniedimagedownloader.wrappeddownloader object? Actually, we just imageloaderconfiguration the Baseimagedownloader object that was passed in the constructor. Look at the GetStream (...) in this class. Method.

@Override PublicInputStream GetStream (String Imageuri, Object Extra)throwsIOException {Switch(Scheme.ofuri (Imageuri)) { CaseHTTP: CaseHTTPS:returngetstreamfromnetwork (Imageuri, extra);  CaseFILE:returngetstreamfromfile (Imageuri, extra);  CaseCONTENT:returngetstreamfromcontent (Imageuri, extra);  CaseASSETS:returngetstreamfromassets (Imageuri, extra);  Casedrawable:returngetstreamfromdrawable (Imageuri, extra);  CaseUNKNOWN:default:                returnGetstreamfromothersource (Imageuri, extra); }    }

From this function, we can see UIL through Scheme.ofuri (...). Analysis Imageuri, according to the type of Imageuri select the corresponding method for processing. By analyzing the scheme class, we found that UIL supports the following image acquisition methods: http, HTTPS, FILE, CONTENT, ASSETS, drawable.

Next, we analyze Slownetworkimagedownloader.getstream (...) method, the download of each picture will eventually pass BitmapFactory.decodeStream解析成Bitmap,供ImageView显示 . We can find that this method is used by Flushedinputstream for slow network processing. The reason for using this class is because in a slow network, BitmapFactory.decodeStream无法正确解析完整的图片。具体的可以参考StackOverFlow上的帖子《 Bitmapfactory.decodestream always returns null and Skia decoder shows Decode returned false》和一个Google上的Bug 报告《BitmapFactory.decodeStream() fails if InputStream.skip() does not skip fully》。

网速不慢的下载就直接使用BaseImageDownloader.getStream(…)方法了。

At this point, we have analyzed the image download technique in UIL, Finally, comb it. In response to slow, normal, access-restricted networks, UIL used Slownetworkdownloader, Baseimageloader, and networkdenieddownloader to address these policies, respectively, In Loadanddisplayimagetask.getdownloader (...) By obtaining the corresponding downloader, and finally through the Loadanddisplayimagetask.decodeimage (...). Parse the picture out.

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.