The package contains the following images:
Where Imagedownloader is the interface, Baseimagedownloader is the implementation class for Imagedownloaer.
Imagedownloader (interface)
This interface provides an interface method to the outside:
InputStream GetStream (String imageuri,object extra) throws IOException;
It is obvious that this method has the effect of getting the input stream of the Imageuri specified picture file.
Since the file source specified by Imageuri can come from a different kind of server or address, the interface also defines an enumeration class scheme to specify what kind of Imageuri the source belongs to, and the types listed are as follows:
|
HTTP ("http"), HTTPS ("https") |
Image from HTTP Server
|
|
File ("file") |
Imageuri as a string for the file protocol |
|
Content ("content") |
Image files from the app's ContentProvider |
|
ASSETS ("ASSETS") |
Images from the assets folder in the app |
|
Drawable ("drawable") |
Images from the app's drawable folder
|
|
UNKNOWN ("") |
The image comes from other means, in which case the RuntimeException exception is thrown |
The enumeration class also provides the following methods:
Ofuri (String uri): This method determines which of the seven enumerated types the picture belongs to, based on the URI of the picture.
Wrap (String Path): This method is used to prefix path with a URI, such as Http://+path
Corp (String URI): with the Wrap method instead, remove URI the prefix
Note: The most important role of this enumeration class is to return different picture file input streams for pictures based on different enumeration types. This is reflected in the implementation class Baseimagedownloader of this interface.
Baseimagedownloader (Implements Imagedownloader)
The main function of this class is to obtain the input stream of the picture file according to Imageuri, which provides several important properties as follows:
|
Field name |
Property |
Field description |
|
Default_http_connect_timeout |
Final int |
Maximum time-out for network links |
|
Default_http_read_timeout |
Final int |
Time-out of reading data from the host |
|
Buffer_size |
Final int |
The size of the cache, 32k |
|
Max_redirect_count |
Final int |
The number of times the link was re-requested when the link network timed out |
|
Content_contacts_uri_prefix |
Final int |
When the picture comes from ContentProvider, use it. |
|
Error_unsupported_scheme |
Final int |
Error message, when Imageuri scheme belongs to unknown, prompt error message |
|
Context |
Context |
Context of the App app |
|
ConnectTimeout |
Int |
Let the user set the link timeout time |
|
ReadTimeout |
Int |
Let the user design the read time-out |
The class provides two constructors:
/** * Use the object set by this constructor to set the default maximum connection time * @param context */public Baseimagedownloader (context context) {This.context = Context.geta Pplicationcontext (); this.connecttimeout = Default_http_connect_timeout;this.readtimeout = DEFAULT_HTTP_READ_ TIMEOUT;} /** * User Set timeout time * @param context * @param connecttimeout * @param readtimeout */public baseimagedownloader (Context Contex T, int connecttimeout, int readtimeout) {this.context = Context.getapplicationcontext (); this.connecttimeout = Connecttimeout;this.readtimeout = ReadTimeout;}
Here's a look at how the class implements the GetStream method of the interface, as follows
Public InputStream GetStream (String Imageuri, Object extra) throws IOException {switch (Scheme.ofuri (Imageuri)) {case HTT P:case Https:return getstreamfromnetwork (Imageuri, extra);//Get network input stream case File:return Getstreamfromfile (Imageuri, extra);//Get file input stream case Content:return getstreamfromcontent (Imageuri, extra);//Get the application context picture file input stream case Assets:return Getstreamfromassets (Imageuri, extra);//Get Assets picture file input stream case Drawable:return getstreamfromdrawable (Imageuri, extra) ;//Get the input stream of the picture file inside the drawable case UNKNOWN:default:return Getstreamfromothersource (Imageuri, extra);//Get the input stream of the picture of the other source}}
The class also provides a createconnection method to get a HttpURLConnection linked object based on ImageUrl
Protected httpurlconnection createconnection (string url, Object extra) throws IOException {string encodedurl = Uri.encode (URL, allowed_uri_chars);//Gets a httpurlconnecttionhttpurlconnection conn = (httpurlconnection) new URL via a URL link ( Encodedurl). OpenConnection (); Conn.setconnecttimeout (connecttimeout); Conn.setreadtimeout (readTimeout); return Conn;}
The input stream object that is returned when Imageuri scheme is Http,https and file is an input stream object that has been decorated (applied in decorative mode) Contentlengthinputstream
The following methods are covered in the GetStream method:
Protected InputStream getstreamfromnetwork (String Imageuri, Object extra) throws IOException {HttpURLConnection conn = cr Eateconnection (Imageuri, extra);//based on Imaguri to get a httpurlconnection object int redirectcount = 0;//If the request is unsuccessful, Request again until the request succeeds or the redirect exceeds the maximum number of requests while (Conn.getresponsecode ()/= 3 && Redirectcount < Max_redirect_count) {conn = CreateConnection (Conn.getheaderfield ("location"), extra); redirectcount++;} InputStream Imagestream;try {//Get the network input stream, with this object to read data from the network ImageStream = Conn.getinputstream ();} catch (IOException e) {// Read all data to allow reuse connection (Http://bit.ly/1ad35PY) Ioutils.readandclosestream (Conn.geterrorstream ()); throw e;} Further encapsulation of the network input stream, which uses the adornment mode return new Contentlengthinputstream (New Bufferedinputstream (ImageStream, Buffer_size), Conn.getcontentlength ());} Protected InputStream Getstreamfromfile (string Imageuri, Object extra) throws IOException {string filePath = Scheme.file. Crop (Imageuri);//get path from URL return to new Contentlengthinputstream (new BUFFEREDINPUtstream (New FileInputStream (FilePath), buffer_size), (int) new File (FilePath). Length ()); Protected InputStream getstreamfromcontent (String Imageuri, Object extra) throws FileNotFoundException {/* * Contentproviders stores and retrieves data, which allows all applications to access, which is the only way to share data between applications, and is responsible for acquiring the data provided by ContentProvider; */contentresolver res = Context.getcontentresolver (); Uri uri = Uri.parse (Imageuri), if (Imageuri.startswith (Content_contacts_uri_prefix)) {return ContactsContract.Contacts.openContactPhotoInputStream (res, URI);} else {return res.openinputstream (URI);}} Protected InputStream getstreamfromassets (string Imageuri, Object extra) throws IOException {string FilePath = Scheme.ass Ets.crop (Imageuri);//Gets the path of the file return Context.getassets (). open (FilePath);//Gets the input stream of the file}protected InputStream Getstreamfromdrawable (String Imageuri, Object extra) {string drawableidstring = Scheme.DRAWABLE.crop (imageuri); int Drawableid = Integer.parseint (drawableidstring); return Context.getresources (). Openrawresource (drawableId);} Protected INPUTSTREAM Getstreamfromothersource (String Imageuri, Object extra) throws IOException {throw new unsupportedoperationexception (String.Format (Error_unsupported_scheme, Imageuri));}