ImageView Display Network pictures

Source: Internet
Author: User

 PackageCom.example.urlimage; ImportJava.io.InputStream; Importjava.net.HttpURLConnection; ImportJava.net.URL; Importandroid.app.Activity; ImportAndroid.graphics.Bitmap; Importandroid.graphics.BitmapFactory; ImportAndroid.os.Bundle; ImportAndroid.os.Handler; ImportAndroid.os.Message; ImportAndroid.view.View; ImportAndroid.widget.Button; ImportAndroid.widget.ImageView;  Public classMainactivityextendsActivity {//Declaration Address    PrivateButton btn; PrivateImageView img; PrivateString url = "http://imgstore04.cdn.sogou.com/app/a/100520024/877e990117d6a7ebc68f46c5e76fc47a"; //implementing changes to controls in Message Queuing    PrivateHandler handle =NewHandler () { Public voidhandlemessage (Message msg) {Switch(msg.what) { Case0: System.out.println ("111"); Bitmap BMP=(Bitmap) msg.obj;                  Img.setimagebitmap (BMP);  Break;      }          };        }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);            Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.ID.BTN); IMG=(ImageView) Findviewbyid (r.id.img); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//New Line loads download picture information, send to Message queue                NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubBitmap BMP =geturlimage (URL); Message msg=NewMessage (); Msg.what= 0; Msg.obj=bmp; System.out.println ("000");                      Handle.sendmessage (msg);              }}). Start ();      }          }); }        //Loading Pictures     PublicBitmap geturlimage (String URL) {Bitmap bmp=NULL; Try{URL Myurl=Newurl (URL); //Get ConnectedHttpURLConnection conn =(HttpURLConnection) myurl.openconnection (); Conn.setconnecttimeout (6000);//Set TimeoutConn.setdoinput (true); Conn.setusecaches (false);//do not cacheConn.connect (); InputStream is= Conn.getinputstream ();//get a picture of the data streamBMP =Bitmapfactory.decodestream (IS);          Is.close (); } Catch(Exception e) {e.printstacktrace (); }          returnbmp; }  }  

ImageView Display Network pictures

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.