Interface Layout linerlayout linear layout,ImageView controls,EditText controls hint Property Tip information, Button control.
android:layout_weight= "" attribute, weight, only when the width and height of the control is 0 to represent the weight, otherwise it represents the priority of the rendering, the higher the value of the lower priority, the default is 0 , the other controls are rendered first
Singleline Property Line
Business logic, get EditText values into ImageView , essentially http get Request
Gets the EditText object, via Findviewbyid ()
Gets the value that calls the toString () method by calling the GetText () method of the EditText object, calling the trim () method
Call the IsEmpty () method of the Textutils class to determine whether it is empty and Toast
Send a get request
Gets the URL object, via the new URL (path), parameter:path is a String the path where the exception needs to be caught
Gets the httpurlconnection object, which is returned by invoking the OpenConnection () method of the Url object. URLConnection object, and then force the type to convert a bit
Call the httpurlconnection object's Setrequestmethod method, set the request method, is GET
Call the setconnecttimeout (Millis) of the httpurlconnection Object, set the time-out,Millis is milliseconds
Call the setrequestproperty (field,newvalue) method of the HttpURLConnection object , set the request parameters, such as the request header, user-agent wait
Call the httpurlconnection object's getresponsecode () method to get the server's response code,
Invokes the getInputStream () method of the httpurlconnection object , gets the input stream, and returns the InputStream Object
Call the Decodestream (IS) method of the Bitmapfactory class to get the Bitmap object, Parameters: is is a InputStream Object
Call the setimagebitmap (bitmap) method of the ImageView object to set the picture, parameter:bitmap Object
All access to the network need to add permissions Android.permission.INTERNET
At this point, the direct deployment, will be reported an error networkonmainthreadexception, does not allow network operation on the main thread, the following code only valid when the lower version
PackageCom.tsh.internetpic;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_path; PrivateImageView Iv_pic; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_path=(EditText) Findviewbyid (R.id.et_path); Iv_pic=(ImageView) Findviewbyid (r.id.iv_pic); } Public voidgetinternetimg (View v) {String path=Et_path.gettext (). toString (). Trim (); if(Textutils.isempty (path)) {Toast.maketext ( This, "Picture path cannot be empty", 0). Show (); }Else{ Try{URL URL=NewURL (path); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); Conn.setrequestproperty ("User-agent", "" "); intCode=Conn.getresponsecode (); if(code==200) {InputStream is=Conn.getinputstream (); Bitmap BP=Bitmapfactory.decodestream (IS); Iv_pic.setimagebitmap (BP); }Else{Toast.maketext ( This, "Network connection timed out", 0). Show (); } } Catch(Exception e) {e.printstacktrace (); } } }}
[Android] Network picture Viewer