When talking about http, you must understand the URI and URL.
URI (uniform resource identifier) unified resource identifier;
URL (uniform resource location) unified resource locator (or unified resource Locator );
It can be understood that a URL is a subset of a URI, and a URI is an abstract identifier. a url can be understood as a specific Identifier. A unique URL can be found as long as it is a resource on the network.
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 {/*** to retrieve network images ** @ param urlString * For example: http://f.hiphotos.baidu.com/image/w%3D2048/sign=3 * b06d28fc91349547e1eef6462769358 */optional * @ 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 the connection HttpURLConnection urlConn = (HttpURLConnection) imgUrl. openConnection (); urlConn. setDoInput (true); urlConn. connect (); // convert the obtained data to InputStreamInputStream is = urlConn. getInputStream (); // converts 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 ;}}To use the network, you must add permissions and have network permissions. Otherwise, the download fails.
Downloading is a time-consuming operation. If it is placed in the UI thread, ANR is easy to appear. Therefore, network operations after Android4.0 cannot be placed in the UI thread. Here I will put the downloading in AsyncTask for processing.
Class downloadImageTask extends AsyncTask
{@ Overrideprotected Boolean doInBackground (String... params) {// TODO Auto-generated method stubSystem. out. println ("[downloadImageTask->] doInBackground" + params [0]); mDownloadImage = HttpUtils. getNetWorkBitmap (params [0]); return true;} // callback for download completion @ 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 also open a thread for processing, making it easier to use AsyncTask.
Complete Demo: http://download.csdn.net/detail/deng0zhaotai/7326005