Loads the Internet pictures and displays them on ImageView. You cannot change the interface layout in a child thread.
Requires permission to connect to the network: <uses-permission android:name= "Android.permission.INTERNET"/>
Layout file:
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity " > <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentleft= "true"Android:layout_alignparenttop= "true"Android:layout_marginleft= "45DP"Android:layout_margintop= "16DP"Android:text= "button" /> <ImageViewAndroid:id= "@+id/imageview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/button1"Android:layout_below= "@+id/button1"Android:layout_marginleft= "24DP"Android:layout_margintop= "80DP"android:src= "@drawable/ic_launcher" /></Relativelayout>
View Code
Activity class:
PackageCom.example.netdemo;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivity {Privatebutton button; PrivateImageView ImageView; PrivateHandler handler=NewHandler () {@Override Public voidhandlemessage (Message msg) {//determine which child thread is based on the value of Msg.what if(msg.what==11) {Bitmap Mbitmap=(Bitmap) msg.obj; Imageview.setimagebitmap (MBITMAP); } } }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button=(Button) Findviewbyid (R.id.button1); ImageView=(ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//Turn on child threadsThread thread=NewThread (Newmyrunnable ()); Thread.Start (); } }); }classMyrunnableImplementsrunnable{@Override Public voidrun () {String urlstring= "Http://amorypepelu.github.io/shell/src/8eea0ecdd3c83d2.jpg"; InputStream is=NULL; Try { //Configure Network ConnectionsURL url=NewURL (urlstring); HttpURLConnection Connection=(HttpURLConnection) url.openconnection (); Connection.setconnecttimeout (5*1000); Connection.setrequestmethod ("GET"); Connection.setrequestproperty ("", ""); is=Connection.getinputstream (); //Parse PictureBitmap bitmap=Bitmapfactory.decodestream (IS); //Configuring MessagesMessage msg=Handler.obtainmessage (); Msg.what=11; Msg.obj=bitmap; //Sendhandler.sendmessage (msg); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{ if(is!=NULL) { Try{is.close (); } Catch(IOException e) {e.printstacktrace (); } } } } }}
Web Image loading