[Android-007] [network image Viewer]
Network Image Viewer Determine the image URL to send an http request
URL url = new URL (address); // gets the connection object and does not establish a connection HttpURLConnection conn = (HttpURLConnection) url. openConnection (); // set connection and read timeout conn. setConnectTimeout (5000); conn. setReadTimeout (5000); // set the request method. Note that conn must be capitalized. setRequestMethod (GET); // establish a connection and send a get request // conn. connect (); // establish a connection and obtain a response. 200 indicates that the request is successfully conn. getResponseCode ();
The server image is returned to the browser in the form of a stream
// Get the InputStream is = conn. getInputStream () returned by the server; // read the data in the stream and construct the image Bitmap bm = BitmapFactory. decodeStream (is );
Set the image to the display content of ImageView.
ImageView iv = (ImageView) findViewById(R.id.iv); iv.setImageBitmap(bm);
Add permission
The main thread cannot be blocked.In Android, the main thread is blocked, causing the application to fail to refresh the ui and respond to user operations. The user experience is very poor. The main thread is blocked for a long time. The system will throw an ANR exception. ANR: application Not Response; the Application does Not respond to any time-consuming operations and cannot be written in the main thread because network interaction is a time-consuming operation. If the network speed is slow, the code will be blocked, therefore, the code for network interaction cannot run in the main thread.
Only the main thread can refresh the uiThe code for refreshing the ui can only run in the main thread. Running the ui in the sub-thread has no effect. If you need to refresh the ui in the sub-thread, use the message queue mechanism.
Message QueueOnce a logoff finds a Message in the Message Queue, it will take out the Message and then throw the Message to the Handler object, handler will call its own handleMessage method to process this message handleMessage method. When the main thread is created, the message queue and the polling device object will be created, but the message processor object will be, create your own
// Message Queue Handler handler = new Handler () {// There Is A looper in the main thread, which constantly checks whether there are new messages in the message queue. If new messages are found, // automatically call this method. Note that this method is the public void handleMessage (android. OS. message msg ){}};
Send messages to the message queue in a subthread
// Create the Message object Message msg = new Message (); // The obj attribute of the Message can be assigned to any object, through which the data msg can be carried. obj = bm; // what attribute is equivalent to a tag used to differentiate messages and run the code msg that is not allowed. what = 1; // send the handler message. sendMessage (msg );
Use switch statements to differentiate messages
Public void handleMessage (android. OS. message msg) {switch (msg. what) {// if it is 1, it indicates that the request is successful. case 1: ImageView iv = (ImageView) findViewById (R. id. iv); Bitmap bm = (Bitmap) msg. obj; iv. setImageBitmap (bm); break; case 2: Toast. makeText (MainActivity. this, request failed, 0 ). show (); break ;}}
Add cache image FunctionRead the data in the stream returned by the server, and write the data to the local file through the file input stream.
// 1. the InputStream is = conn. getInputStream (); // 2. read the data in the stream and construct the image FileOutputStream fos = new FileOutputStream (file); byte [] B = new byte [1024]; int len = 0; while (len = is. read (B ))! =-1) {fos. write (B, 0, len );}
Code for creating a bitmap object is changed
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath());
Check whether an image with the same name exists in the cache before each request is sent. If so, read the cache.
Obtain open-source websitesCode.google.com github.com search for smart-image-view on github to download the open-source project smart-image-view. When using custom components, the label name must be the package name.
Usage of SmartImageView
SmartImageView siv = (SmartImageView) findViewById(R.id.siv); siv.setImageUrl(http://192.168.1.102:8080/dd.jpg);