Android uses imageview to display local and online images

Source: Internet
Author: User
Imageview is android Program A commonly used component that displays an image on the screen. Define an imageview in Ui XML as follows: public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. myimage); imageview image1 = (imageview) findviewbyid (R. myimage. image); // Bitmap bitmap = getloacalbitmap ("/AA/aa.jpg"); // Bitmap bitmap = gethttpbitmap ("http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg "); // obtain the image image1 from the Internet. setimagebitmap (Bitmap ); // Set bitmap}/*** load local image * http://bbs.3gstdy.com * @ Param URL * @ return */public static bitmap getloacalbitmap (string URL) {try {fileinputstream FCM = new fileinputstream (URL); Return bitmapfactory. decodestream (FS);} catch (filenotfoundexception e) {e. printstacktrace (); return NULL ;}/ *** fetch image from server * http://bbs.3gstdy.com * @ Param URL * @ return */public static bitmap gethttpbitmap (string URL) {URL myfileurl = NULL; Bitmap bitmap = NULL; try {log. D (TAG, URL); myfileurl = new URL (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 ();} return bitmap ;}
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: 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.