Android: how to display network images

Source: Internet
Author: User
Displaying a network image in Android is actually super simple. The following is a very simple example: Step 1: 1. Create your activity. In this example, viewwebimageactivity is described. 2. In viewwebimageactivity Code String imageurl = ""; // This is the network picture you need to display --- the bitmap bmimg; imageview imview; button button1; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); imview = (imageview) findviewbyid (R. id. imview); imview. setimagebitmap (returnbitmap (imageurl);} public bitm AP returnbitmap (string URL) {URL myfileurl = NULL; Bitmap bitmap = NULL; try {myfileurl = new URL (URL);} catch (malformedurlexception e) {e. printstacktrace ();} Try {httpurlconnection conn = (httpurlconnection) myfileurl. openconnection (); Conn. setdoinput (true); Conn. connect (); inputstream is = Conn. getinputstream (); bitmap = bitmapfactory. decodestream (is); is. close ();} catch (ioexception E) {E. printstacktrace () ;}return bitmap;} 3. The returnbitmap (string URL) method converts a network image to a bitmap. Step 2: 1. Modify your main. xml file as follows: <? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <imageview Android: id = "@ + ID/imview" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center"/> </linearlayout> Step 3: 1. In your androidmanifest. add <uses-Permission Android: Name = "android. Permission. Internet"/>. This is because android has many permission restrictions. Otherwise, Images cannot be displayed on your simulator.
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.