Android Learning eight: Get network pictures

Source: Internet
Author: User

See QQ Group has a friend said loading picture memory overflow problem, so just follow your own ideas to try. But according to his method, I do not know why there is no memory overflow, do not know what the situation.

There are three main purposes of writing this article:

1. Multi-threaded Learning

2. Picture Loading Learning

3. Programming practiced hand

Okay, on the code.

 PackageOrg.tonny;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportAndroid. R;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.widget.ImageView; Public classNetpictactivityextendsactivity{PrivateBitmap Mbitmap =NULL; PrivateImageView MView =NULL; PrivateHandler Mhandler =NULL; //a random image on the internet    Private FinalString MUri = "Http://pic5.nipic.com/20100118/3822085_173909557153_2.jpg"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //this way there is a problem with the R.id.layout method, so we have to use numerical values instead ofSetcontentview (0x7f030000); MView= (ImageView) This. Findviewbyid (0x7f050000); Mhandler=NewHandler () {@Override Public voidhandlemessage (Message msg) {Switch(msg.what) { Case1:                    if(MView = =NULL|| Mbitmap = =NULL)                    {                        return;                    } mview.setimagebitmap (Mbitmap);  Break;    }            }        }; }     Public voidOnbtnshowclick (View v) {Thread Thrad=NewThread () {@Override Public voidrun () {Try{URL URL=NewURL (MUri); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setdoinput (true);                    Conn.connect (); InputStream InputStream=Conn.getinputstream (); //Mbitmap is a global variable that stores the data for a pictureMbitmap =Bitmapfactory.decodestream (InputStream); Message msg=Mhandler.obtainmessage (); //Note that only the type is transmitted hereMsg.what = 1;                    Mhandler.sendmessage (msg);                Inputstream.close (); }                Catch(malformedurlexception e) {e.printstacktrace (); }                Catch(IOException e) {e.printstacktrace ();        }            }        };    Thrad.start (); }}

Note Configure permissions to access the network in the manifest file

All right, keep studying.

Android Learning eight: Get network pictures

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.