Today to implement a simple Web image viewer under Android
The interface is as follows:
The code is as follows:
<LinearLayoutxmlns: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:orientation= "vertical"Tools:context=". Mainactivity " > <ImageViewAndroid:id= "@+id/iv"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_weight= "+" /> <EditTextAndroid:singleline= "true"Android:text= "Http://img01.sogoucdn.com/app/a/100540002/1047586.jpg"Android:id= "@+id/et_path"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:hint= "Please enter picture path" /> <ButtonAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Browse" /></LinearLayout>
The code is as follows:
PackageCom.wuyudong.imagesviewer;ImportJava.io.InputStream;Importjava.net.HttpURLConnection;Importjava.net.MalformedURLException;ImportJava.net.URL;Importorg.apache.http.HttpConnection;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;Importandroid.text.TextUtils;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateEditText Et_path; PrivateImageView IV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Et_path=(EditText) Findviewbyid (R.id.et_path); IV=(ImageView) Findviewbyid (R.ID.IV); } Public voidClick (View view) {String path=Et_path.gettext (). toString (). Trim (); if(Textutils.isempty (path)) {Toast.maketext ( This, "Picture path cannot be empty", 0). Show (); } Else { //Connect server GET request get picture Try{URL URL=NewURL (path); //request to send HTTP from URLHttpURLConnection conn =(httpurlconnection) URL. OpenConnection (); //How to set the requestConn.setrequestmethod ("GET"); Conn.setconnecttimeout (5000); Conn.setreadtimeout (5000); Conn.setrequestproperty ("User-agent", "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/45.0.2454.101 safari/537.36 "); intCode =Conn.getresponsecode (); if(Code = = 200) {InputStream is=Conn.getinputstream (); Bitmap Bitmap=Bitmapfactory.decodestream (IS); Iv.setimagebitmap (bitmap); } Else{Toast.maketext ( This, "Show Picture Failed", 0). Show (); } } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); Toast.maketext ( This, "Access failed to get picture", 0). Show (); } } }}
Add Permission: Android.permission.INTERNET
Error after operation: android.os.NetworkOnMainThreadException
To explain, from the Honeycomb SDK (3.0), Google no longer allows the network request (HTTP, Socket) and other related operations directly in the main thread class, in fact, should not do so, directly in the UI thread network operation, will block the UI, The user experience is quite poor!
So, in the Honeycomb SDK (3.0) The following version, you can continue to do so in main thread, at more than 3.0, will not
Concrete solutions See the following article
Android Network Picture Viewer