In the project, getting the network data is a very common thing, all kinds of data on the server side or direct print, or the entity class thrown into the list into JSON, by the client using the HttpClient execute () method to receive, to complete the process of data interaction.
For the picture, the landlord take the use of the URL to obtain a Picture object method to obtain:
Getpicture.java
Import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Org.apache.http.httpresponse;import Org.apache.http.httpstatus;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.params.coreconnectionpnames;import Java.io.ioexception;import java.io.inputstream;/** * Get network Picture tool class * Pictureurl: Image URL, network address, server path */public class getpicture {private Bitmap picture; Public Bitmap getnetpicture (final String pictureurl) {new Thread (new Runnable () {@Override public void Run () {Bitmap img = null; HttpClient client = null; HttpGet request = null; HttpResponse response = null; try {request = new HttpGet (Pictureurl); Client = new Defaulthttpclient (); Client.gEtparams (). Setparameter (Coreconnectionpnames.connection_timeout, 1000 * 5); Link Timeout client.getparams (). Setparameter (coreconnectionpnames.so_timeout,1000 * 5); Read timeout response = Client.execute (request); if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {InputStream is = Response.geten Tity (). getcontent (); IMG = Bitmapfactory.decodestream (IS); Showtoast ("Network Picture:" +pictureurl+ "get success! "); } else {showtoast ("Network Picture:" +pictureurl+ "Get failed! "); }} catch (Clientprotocolexception e) {e.printstacktrace (); Showtoast (E.getmessage ()); } catch (IOException e) {e.printstacktrace (); Showtoast (E.getmessage ()); } picture = img; }}). Start (); Return PICTUre } private void Showtoast (String words) {System.out.println (words); }}As shown in the code, access the URL to get the picture object, use the file stream to implement the cache to memory, and finally return to the desired place
How to use:
Get Picture Object
String url = "Picture url";
Getpicture getpicture = new Getpicture ();
Bitmap picture = getpicture.getnetpicture (URL);
Mainactivity.java:
Import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.widget.button;import Android.widget.ImageView; Import Com.absolom.getnetpicture.tools.getpicture;public class Mainactivity extends Appcompatactivity {Button GetPict Ure1; Button GetPicture2; ImageView ImageView; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); String URL1 = "Http://e.hiphotos.baidu.com/zhidao/pic/item/8cb1cb1349540923f12939199458d109b3de4910.jpg"; String url2 = "Http://e.hiphotos.baidu.com/zhidao/pic/item/aec379310a55b31907d3ba3c41a98226cffc1754.jpg"; ImageView = (ImageView) Findviewbyid (R.id.imageview); Getpicture getpicture = new Getpicture (); GetPicture1 = (Button) Findviewbyid (r.id.getpicture1); GetPicture2 = (Button) Findviewbyid (r.id.getpicture2); Getpicture1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Imageview.setimagebitmap (Getpicture.getnetpicture (URL1)); } }); Getpicture2.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Imageview.setimagebitmap (Getpicture.getnetpicture (URL2)); } }); }}
Android Get network pictures