Android uses ImageView to display local and online images

Source: Internet
Author: User

ImageView is a frequently used component of an Android program that displays a picture on the screen. A imageview is defined in the UI XML as follows:
 Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);     Setcontentview (R.layout.myimage); ImageView Image1=(ImageView) Findviewbyid (r.myimage.image); //Bitmap Bitmap = Getloacalbitmap ("/aa/aa.jpg");//take pictures from localBitmap Bitmap =Gethttpbitmap ("Http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg");//take pictures from the InternetImage1. Setimagebitmap (bitmap);//Set Bitmap}/*** Load Local image *http://bbs.3gstdy.com* @paramurl*@return*/ Public StaticBitmap getloacalbitmap (String url) {Try{FileInputStream fis=Newfileinputstream (URL); returnBitmapfactory.decodestream (FIS); } Catch(FileNotFoundException e) {e.printstacktrace (); return NULL; }}/*** Take pictures from the server *http://bbs.3gstdy.com* @paramurl*@return*/ Public StaticBitmap gethttpbitmap (String url) {URL myfileurl=NULL; Bitmap Bitmap=NULL; Try{log.d (TAG, URL); Myfileurl=Newurl (URL); } Catch(malformedurlexception e) {e.printstacktrace (); }     Try{HttpURLConnection conn=(HttpURLConnection) myfileurl.openconnection (); Conn.setconnecttimeout (0); Conn.setdoinput (true);          Conn.connect (); InputStream is=Conn.getinputstream (); Bitmap=Bitmapfactory.decodestream (IS);     Is.close (); } Catch(IOException e) {e.printstacktrace (); }     returnbitmap;}
View Code

Android uses ImageView to display local and online images

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.