When it comes to HTTP, you need to know the URI and URL
URI (Uniform Resource Identifier) uniform Resource identifier;
URL (Uniform Resource location) Uniform Resource Locator (or Uniform Resource Locator);
It can be understood that a URL is a subset of URIs, the URI is an abstract identifier, the URL can be interpreted as a specific identifier, as long as the resources on the network can find a unique URL.
Directory structure
Key code
Httputils.java class
Package Com.dzt.downloadimage.utils;import Java.io.ioexception;import Java.io.inputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import Java.net.url;import Android.graphics.bitmap;import android.graphics.bitmapfactory;/** * HTTP Tool class HTTP can use HttpURLConnection or httpclient * * @author Administrator * @date 2014.05.10 * @version V1.0 */public class Httputils {/** * Get network pictures * * @param urlstring * such as: http://f.hiphotos.baidu.com/image/w%3D2048/sign=3 * b06d28fc91349547e1eef6462769358 */d0 00baa1cd11728b22c9e62ccafcc3cec2fd2cd3.jpg * @return * @date 2014.05.10 */public static Bitmap Getnetworkbitmap (String urlstring) {URL imgurl = null; Bitmap Bitmap = null;try {imgurl = new URL (urlstring);//Use HttpURLConnection to open connection httpurlconnection Urlconn = (httpurlconne ction) imgurl.openconnection (); Urlconn.setdoinput (true); Urlconn.connect ();//convert the resulting data into inputstreaminputstream is = Urlconn.getinputstream ();//convert InputStream to Bitmapbitmap = BitmapfActory.decodestream (IS); Is.close ();} catch (Malformedurlexception e) {//TODO auto-generated catch BlockSystem.out.println ("[Getnetworkbitmap->] Malformedurlexception "); E.printstacktrace ();} catch (IOException e) {System.out.println ("[getnetworkbitmap->]ioexception"); E.printstacktrace ();} return bitmap;}}Use to network must add permissions and to have network, otherwise download failed
<uses-permission android:name= "Android.permission.INTERNET"/>
Downloading is a time-consuming operation, and if placed in the UI thread is prone to ANR, so the operation on the network after Android4.0 cannot be placed on the UI thread, where I put the download in asynctask processing.
Class Downloadimagetask extends Asynctask<string, Integer, boolean> {@Overrideprotected Boolean doinbackground ( String ... params) {//TODO auto-generated method StubSystem.out.println ("[Downloadimagetask->]doinbackground" + Params[0]); mdownloadimage = Httputils.getnetworkbitmap (Params[0]); return true;} Download complete callback @overrideprotected void OnPostExecute (Boolean result) {//TODO auto-generated method Stubimage.setimagebitmap ( Mdownloadimage); SYSTEM.OUT.PRINTLN ("result =" + result); Super.onpostexecute (result);} Update progress callback @overrideprotected void Onprogressupdate (Integer ... values) {//TODO auto-generated method Stubsuper.onprogressupdate (values);}}
In OnPostExecute and onprogressupdate we can easily update the UI, of course, we can open a thread processing, the use of asynctask is more convenient.
Full demo:http://download.csdn.net/detail/deng0zhaotai/7326005