Android displays online images

Source: Internet
Author: User

Import Java. io. bytearrayoutputstream; <br/> Import Java. io. inputstream; <br/> Import httpurlconnection; <br/> Import URL; </P> <p> Import android. app. activity; <br/> Import android. graphics. bitmap; <br/> Import android. graphics. bitmapfactory; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. edittext; <br/> import Droid. widget. imageview; <br/> Import android. widget. toast; </P> <p> public class androidtest extends activity {<br/> Private Static final string url = ""; <br/> private edittext pathtext; <br/> private imageview; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> sup Er. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> pathtext = (edittext) This. findviewbyid (R. id. path); <br/> pathtext. settext (URL); <br/> imageview = (imageview) This. findviewbyid (R. id. imageview); <br/> button = (button) This. findviewbyid (R. id. button); <br/> button. setonclicklistener (new view. onclicklistener () {</P> <p> Public void onclick (view v) {<br/> string pat H = pathtext. gettext (). tostring (); <br/> try {<br/> byte [] DATA = getimage (PATH); <br/> If (Data! = NULL) {<br/> Bitmap bitmap = bitmapfactory. decodebytearray (data, 0, Data. length); // bitmap <br/> imageview. setimagebitmap (Bitmap); // display image <br/>} else {<br/> toast. maketext (androidtest. this, "Image Error! ", 1 ). show (); <br/>}< br/>} catch (exception e) {<br/> toast. maketext (androidtest. this, "newwork error! ", 1 ). show (); <br/> E. printstacktrace (); <br/>}< br/> }); </P> <p >}</P> <p>/** <br/> * Get image from newwork <br/> * @ Param path the path of image <br/> * @ return <br/> * @ throws exception <br/> */<br/> Public static byte [] getimage (string path) throws exception {<br/> URL url = new URL (PATH); <br/> httpurlconnection conn = (httpurlconnection) URL. openconnection (); <br/> Conn. setconnectti Meout (5*1000); <br/> Conn. setrequestmethod ("get"); <br/> inputstream instream = Conn. getinputstream (); <br/> If (Conn. getresponsecode () = 200) {<br/> return readstream (instream); <br/>}< br/> return NULL; <br/>}</P> <p>/** <br/> * get data from stream <br/> * @ Param instream <br/> * @ return <br/> * @ throws exception <br/> */<br/> Public static byte [] readstream (inputstream instream) throws exce Ption {<br/> bytearrayoutputstream outstream = new bytearrayoutputstream (); <br/> byte [] buffer = new byte [1024]; <br/> int Len = 0; <br/> while (LEN = instream. read (buffer ))! =-1) {<br/> outstream. write (buffer, 0, Len); <br/>}< br/> outstream. close (); <br/> instream. close (); <br/> return outstream. tobytearray (); <br/>}</P> <p >}< br/>

Add network permissions in manifest. xml
 <Uses-Permission Android: Name = "android. Permission. Internet"/>


Related content: Android multi-thread remote image download


Related Article

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: 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.