Reprint Please specify Source: http://blog.csdn.net/bettarwang/article/details/41229955
The URL (Uniform Resource Locator) object represents a Uniform resource locator, which is a pointer to an Internet resource. The URL consists of the protocol name, host, port, and resource path component, which satisfies the following format:
Protocol://host:port/path
For example, http://kan.sogou.com/dianying/is a URL address.
The URL provides several construction methods for creating a URL object, and it provides the following main methods:
(1) String getFile (): Gets the resource name for this URL;
(2) String gethost (): Gets the host name of this URL;
(3) String GetPath (): Gets the path portion of this URL;
(4) String Getprotocol (): Gets the protocol name for this URL;
(5) String Getquery (): Gets the query string portion of this URL;
(6) URLConnection openconnection (): Returns a URLConnection object that represents the connection of the remote object referenced by the URL to the process;
(7) InputStream OpenStream (): Opens a connection to this URL and returns a inputstream that is used to read the URL resource;
Below is an example of a picture of death on Baidu, which demonstrates how to use a URL:
Layout file is very simple, on the two button and a imageview, do not stick up, the following is the source:
public class Mainactivity extends Activity implements view.onclicklistener{private static final String pic_url= "/http/ Pic7.nipic.com/20100528/4981527_163140644317_2.jpg ";p rivate static final int show_image=0x123;private static final int save_succ=0x124;private static final int save_error=0x125; Button Showimagebutton,saveimagebutton;imageview ImageView; Bitmap bitmap;private Handler handler=new Handler () {@Overridepublic void Handlemessage (Message msg) {if (msg.what==show _image) {imageview.setimagebitmap (bitmap);} else if (MSG.WHAT==SAVE_SUCC) {Toast.maketext (Getbasecontext (), "Picture saved successfully", Toast.length_long). Show (); else if (msg.what==save_error) {Toast.maketext (Getbasecontext (), "Picture Save Error", Toast.length_long). Show ();}}; private void Initview () {showimagebutton= (button) Findviewbyid (R.id.showimagebutton); saveimagebutton= (Button) Findviewbyid (R.id.saveimagebutton); imageview= (ImageView) Findviewbyid (R.id.imageview);} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestATE); Setcontentview (R.layout.activity_main); Initview (); Showimagebutton.setonclicklistener (this); Saveimagebutton.setonclicklistener (this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} private void ShowImage () {new Thread () {@Overridepublic void run () {try {URL url = new URL (pic_url); InputStream Is=url.opens Tream (); Bitmap=bitmapfactory.decodestream (is); Handler.sendemptymessage (show_image); Is.close ();} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}. Start ();} public void SaveImage () {new Thread () {@Overridepublic void run () {Try{url url=new URL (pic_url); InputStream is= Url.openstream (); Bufferedinputstream bis=new Bufferedinputstream (IS); String filepath= "Animation.png"; FileOutputStream Fos=getbasecontext (). Openfileoutput (Filepath,mode_private); Bufferedoutputstream bos=new bufferedoutputstream (FOS); Byte[]buff=new byte[32];int Hasread=0;while ((Hasread=bis.read (Buff)) >0) {bos.write (buff,0,hasread);} Bos.close (); Fos.close (); Bis.close (); Is.close (); Handler.sendemptymessage (SAVE_SUCC);} catch (Exception ex) {handler.sendemptymessage (save_error); Ex.printstacktrace ();}}}. Start ();} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.id.showimagebutton: ShowImage (); break;case r.id.saveimagebutton:saveimage (); break;}}}
Saved pictures can be seen under Data/data/com.android.urlsample/files, one of the details to note is that the order of closing the stream is the opposite of the order created, otherwise an exception will be thrown.
Network Programming in Android Series (i): URLConnection