Interface:
Code xml:
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:layout_width= "Match_parent"4android:layout_height= "Match_parent"5android:orientation= "Vertical"6tools:context= "Com.zzw.watctImage.MainActivity" >7 8<ImageView9Android:id= "@+id/image_view"TenAndroid:layout_width= "Match_parent" Oneandroid:src= "@drawable/t01a7b264978bb316f1" Aandroid:layout_height= "Wrap_content" -android:layout_weight= "1"/> - the<LinearLayout -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -android:orientation= "Horizontal" > + -<EditText +Android:id= "@+id/url_edit" AAndroid:layout_width= "Wrap_content" atandroid:layout_height= "Match_parent" -android:layout_weight= "1" -Android:singleline= "true" -android:text= "Http://p1.so.qhimg.com/t017cf2863df563308b.jpg"/> - -<Button inAndroid:id= "@+id/get" -Android:layout_width= "Wrap_content" toandroid:layout_height= "Match_parent" +android:text= "Get Pictures" -Android:textcolor= "@android: Color/holo_blue_light"/> the</LinearLayout> * $</LinearLayout>
XML
A picture of the edited URL in Java code by clicking the Get Picture button
Java code:
1 PackageCom.zzw.watctImage;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.net.HttpURLConnection;6 Importjava.net.MalformedURLException;7 ImportJava.net.URL;8 Importjava.net.URLConnection;9 Ten Importandroid.app.Activity; One ImportAndroid.graphics.Bitmap; A Importandroid.graphics.BitmapFactory; - ImportAndroid.os.Bundle; - ImportAndroid.os.Handler; the ImportAndroid.os.Message; - ImportAndroid.view.Menu; - ImportAndroid.view.MenuItem; - ImportAndroid.view.View; + ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.Button; + ImportAndroid.widget.EditText; A ImportAndroid.widget.ImageView; at - Public classMainactivityextendsActivityImplementsOnclicklistener { -ImageView Image_view =NULL; -EditText Image_uri =NULL; -Handler Handler =NewHandler () { - @Override in Public voidhandlemessage (Message msg) { - //TODO auto-generated Method Stub to Super. Handlemessage (msg); + if(Msg.what = = 0) { -Bitmap Bitmap =(Bitmap) msg.obj; the Image_view.setimagebitmap (bitmap); * } $ Panax Notoginseng } - the }; + /** A * 1. Access network cannot be placed directly inside the main method (Android.os.NetworkOnMainThreadException), should be placed in a thread the * 2.android.view.viewrootimpl$calledfromwrongthreadexception: + * View can only be modified in the main thread or UI thread, should be resolved by communication - */ $ @Override $ protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); the init (); - }Wuyi the //Initialize - Public voidinit () { WuImage_view =(ImageView) Findviewbyid (R.id.image_view); -Image_uri =(EditText) Findviewbyid (r.id.url_edit); AboutFindviewbyid (R.id.get). Setonclicklistener ( This); $ } - - @Override - Public voidOnClick (View v) { A FinalString URI =Image_uri.gettext (). toString (); + NewThread (NewRunnable () { the @Override - Public voidrun () { $ //TODO auto-generated Method Stub theBitmap Bitmap =Getbitmap (URI); the if(Bitmap! =NULL) { theMessage msg =NewMessage (); theMsg.what = 0;//explain who you are . -Msg.obj =bitmap; in handler.sendmessage (msg); the } the About } the }). Start (); the ; the } + - //bitmap------> Bitmaps the PublicBitmap getbitmap (String uri) {BayiHttpURLConnection conn =NULL; the Try { the //1, get the URL of the picture -URL url =NewURL (URI); - the //2. Get the network connection theconn =(HttpURLConnection) url.openconnection (); the the //3, set some common parameters of the request -Conn.setreadtimeout (3000);//set the maximum time for a connection to read data theConn.setconnecttimeout (3000);//Set Timeout theConn.setdoinput (true);//setting requests allows the server to write data the 94 //4, the real request picture, and then the request from the server to save the binary stream into the InputStream inside the Conn.connect (); theInputStream in =Conn.getinputstream (); theBitmap Bitmap =Bitmapfactory.decodestream (in);98 returnbitmap; About}Catch(malformedurlexception e) { - //TODO auto-generated Catch block101 e.printstacktrace ();102}Catch(IOException e) {103 //TODO auto-generated Catch block104 e.printstacktrace (); the }106 //5. Close the network connection107 finally {108 if(Conn! =NULL) {109 Conn.disconnect (); the }111 } the return NULL;113 } the}
JAVA
Get the picture by URL and show it on ImageView