Android Development Series (12): Get images on the network
The process of obtaining a network slice:
Get the image Url-> connect to the image object through the Url-> write the binary data of the image to the memory-> return the data in the memory-> display it
First, we need to add permissions.
Because we want to access the network, we need to add Network Access Permissions in the AndroidManifest. xml file:
Then the page is set: main. xml:
The following is the MainActivity. java file:
Package cn. itcast. image; import cn. itcast. service. imageService; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. editText; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {private EditText pathText; private ImageView imageView; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); pathText = (EditText) this. findViewById (R. id. imagepath); imageView = (ImageView) this. findViewById (R. id. imageView); Button button = (Button) this. findViewById (R. id. button); button. setOnClickListener (new ButtonClickListener ();} private final class ButtonClickListener implements View. onClickListener {public void onClick (View v) {String path = pathText. getText (). toString (); // get the url path of the text box try {// get the binary data of the image byte [] data = ImageService. getImage (path); // converts an image to a bitmap type. Bitmap bitmap = BitmapFactory. decodeByteArray (data, 0, data. length); imageView. setImageBitmap (bitmap); // display image} catch (Exception e) {e. printStackTrace (); Toast. makeText (getApplicationContext (), R. string. error, 1 ). show ();}}}}
Then we need to configure the ImageService. java file:
Package cn. itcast. service; import java. io. inputStream; import java.net. httpURLConnection; import java.net. URL; import cn. itcast. utils. streamTool; public class ImageService {/*** obtain network image data * @ param path network image path * @ return */public static byte [] getImage (String path) throws Exception {// get urlURL url = new URL (path); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); // The HTTP-based connection object conn. setConnectTimeout (5000); conn. setRequestMethod ("GET"); // set to submit if (conn. getResponseCode () = 200) {// If the response code is 200, the request is successful. InputStream inStream = conn. getInputStream (); // get the input stream return StreamTool. read (inStream); // obtain the binary data of the image} return null ;}}
Finally, we need to add a tool class:
Package cn. itcast. utils; import java. io. byteArrayOutputStream; import java. io. inputStream; public class StreamTool {/*** read data in the stream * @ param inStream * @ return * @ throws Exception */public static byte [] read (InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream (); byte [] buffer = new byte [1024]; int len = 0; while (len = inStream. read (buffer ))! =-1) {// inStream. read (buffer) returns int: the length of the data read. When read-1 is read, outStream is finished. write (buffer, 0, len); // write read data to memory} inStream. close (); return outStream. toByteArray (); // return data in memory }}
As follows: