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:
<! -- Access internet permissions --> <uses-permission android: name = "android. permission. INTERNET"/>
Then the page is set: main. xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/imagepath" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://b.hiphotos.baidu.com/image/w%3D310/sign=1d7f12db95dda144da096ab382b5d009/8d5494eef01f3a293c5d63069b25bc315d607c64.jpg" android:id="@+id/imagepath" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" /></LinearLayout>
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: