Android-ImageView Get online picture resources through bitmap

Source: Internet
Author: User

1.

2. Implementing the Code

1. Configure permissions It's one thing that's easy to forget.


<uses-permission android:name= "Android.permission.INTERNET"/> <!--access to INTERNET permissions--

2. Layout files

    

<imageview        android:id= "@+id/imageview1"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:layout_marginbottom= "196DP"        android:src= "@drawable/ic_launcher"/>

3. java files

public class Mainactivity extends Activity {private list<string> urllist = new arraylist<string> ();p rivate Bi TMap bitmap;imageview imageview;imageview imageview2;private URL url = null;/** * notification message */handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {if (Msg.what = = 0x9527) {//display pictures downloaded from the Web imageview.setimagebitmap (bitmap );}}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);//Create and start a new thread to download pictures from the network Urllist.add (" New Thread () {@Overridepublic void run () {try {//Http://pic1.nipic.com/2008-10-22/2008102293811572_2.jpg Create a URL object url = new URL (urllist.get (0));//open URL corresponding to the resource input stream InputStream is = Url.openstream ();//parse the picture from the InputStream stream bitmap = Bitmapfactory.decodestream (IS);//Imageview.setimagebitmap (bitmap);//Send a message informing the UI component to display a picture handler.sendemptymessage ( 0x9527);//Close input stream is.close ();} catch (Exception e) {e.printstacktrace ();}}}. Start ();}}

Android-ImageView Get online picture resources through bitmap

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.