Android Network picture Viewer

Source: Internet
Author: User
Tags http request message queue thread

Project Source Download

Https://github.com/Wang-Jun-Chao/AndroidProjects

Network Picture Viewer

Determine the URL of the picture

Send HTTP request

        URL url =  url (address);
        
        HttpURLConnection conn = (httpurlconnection) url.openconnection ();
        
        Conn.setconnecttimeout ();
        Conn.setreadtimeout ();
        
        Conn.setrequestmethod ();
        
        
        
        Conn.getresponsecode ();

The picture of the server is returned to the browser as a stream

        
        InputStream  = Conn.getinputstream ();
        
        Bitmap BM = Bitmapfactory.decodestream ();

Set the picture to the ImageView display

        ImageView IV = (ImageView) Findviewbyid (R)
        IV (BM)

Add permissions

Main thread cannot be blocked

In Android, the main thread is blocked, which causes the application to not refresh the UI interface, can not respond to user actions, the user experience will be very poor

Main thread blocking time is too long, the system throws ANR exception

Anr:application not Response; application unresponsive

No time-consuming operation can be written in the main thread

Because the network interaction is a time-consuming operation, if the speed is slow, the code will block, so the network interaction code can not run in the main thread

Only the main thread can refresh the UI

The code that refreshes the UI can only run on the main thread, and running on a child thread has no effect

If you need to refresh the UI in a child thread, use the Message Queuing mechanism

Message Queuing

Looper once you find messages in the message queue, they will take the message out and throw the message to the handler object, and handler will call its own Handlemessage method to handle the message

The Handlemessage method runs on the main thread

The message queue and the polling object are created when the main thread is created, but the Message Processor object, when used, creates itself

        
        Handler Handler =  Handler () {
            
            
              (android.os.Message msg) {

            }
        };

Send messages to message queues in child threads

        
         msg = new ();
        
        Msg.obj = BM;
        
        Msg.what =;
        
        Handler.sendmessage (msg);

To differentiate different messages by using a switch statement

        public void Handlemessage (on Android msg) {
            switch (msg) {
            //If yes, description of the message that belongs to the successful request case
            :
                ImageView IV = ( ImageView) Findviewbyid (R)
                Bitmap BM = (Bitmap) msg
                IV (BM) case
                
            :
                Toast (Mainactivity,,) ()
                
            }< c14/>}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

The ability to add cached pictures

Read the data from the stream returned by the server and write it to the local file via the file input stream

        
        InputStream  = Conn.getinputstream ();
        

        FileOutputStream fos =  fileoutputstream (file);
        [] B =  [];
         Len =;
        (len =. Read (b))!=-) {
            Fos.write (b,, Len);
        }

The code to create the bitmap object is changed to

        Bitmap BM = bitmapfactory (file ())

Detects if a picture of the same name exists in the cache before each request is sent, and reads the cache if it exists

Get a Web site for open source code

Code.google.com

Github.com

Search Smart-image-view in GitHub

Download Open Source Project Smart-image-view

Label name to write package name when using custom component

        </>

The use of Smartimageview

        Smartimageview Siv = (smartimageview) Findviewbyid (R)
        Siv ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.