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