Android asynchronous download of network images (2)

Source: Internet
Author: User

In the previous section, we used handler, thread/runnable, URL, httpurlconnection, and so on to asynchronously download network images.

However, using this method has the following Disadvantages:

  • The thread overhead is large. If each task needs to create a threadProgramEfficiency is much lower.
  • Threads cannot be managed. Anonymous threads are not controlled by the program after they are created and started. If many requests are sent, many threads are started and the system is overwhelmed.
  • In addition, we have seen that handler must be introduced to update the UI in the new thread.CodeIt looks very bloated.
  • Is there a better implementation method? This can be! It is asynctask.

    Asynctask is characterized by running tasks outside the main UI thread, while the callback method is in the main UI thread, which effectively avoids the trouble caused by handler.

    Asynctask defines three generic types: Params, SS, and result.
      • Input parameters of Params startup task execution.
      • Percentage of progress background tasks.
      • Result: The result returned by the background task execution.

    Of course, it must also overwrite some of its abstract methods.

     Doinbackground (Params ...)Execute task

    Onpostexecute (result) returns the result of the task execution. Generally, the UI is updated.

     Onprogressupdate (Progress... values)Progress update

    Note: Red is required.

    Step 1: Design the UI, just like in the previous section

    Step 2: same as in the previous section.

    Step 3: instantiate asynctask and execute (Params)

    We must inherit asynctask and override some of its methods. Here we mainly want to get the network image and save it as bitmap so that the UI can be updated based on bitmap.

    Set the returned type parameters for asynctask to string, integer, and bitmap as follows:

    View code

     
    Public ClassMyasynctaskExtendsAsynctask<String, integer, bitmap>{

    InIn the doinbackground (Params...) method, the string... Params is accepted and the bitmap we need is returned. Of course, bitmap is obtained here, So bitmap is returned.

     If you need to return a string or other complex types, you need to modify the class definition parameter type to return the type you need. Of course, the acceptance parameter is also changed based on your request needs.

    View code

     @ Override
    Protected Bitmap doinbackground (string... Params ){
    Bitmap bitmap = Null ;
    Try {

    URL = New URL (Params [ 0 ]);
    Httpurlconnection con = (Httpurlconnection) URL. openconnection ();
    Con. setdoinput ( True );
    Con. Connect ();
    Inputstream = Con. getinputstream ();

    Bitmap = Bitmapfactory. decodestream (inputstream );
    Inputstream. Close ();
    }
    Catch (Malformedurlexception e ){
    E. printstacktrace ();
    } Catch (Ioexception e ){
    // Todo auto-generated Catch Block
    E. printstacktrace ();
    }
    Return Bitmap;
    }

    InOnpostexecute (result) is the UI part updated after the request obtains the result. You will see that its parameter is the type parameter in our class. The Code is as follows:

    View code

     //  After obtaining the image data, update the UI: display the image and hide the progress bar.  
    @ Override
    Protected Void Onpostexecute (Bitmap result ){
    Imageview imgview = (Imageview) This . Viewgroup. getchildat ( 0 );
    Imgview. setimagebitmap (result );
    Progressbar bar = (Progressbar) This . Viewgroup. getchildat ( 1 );
    Bar. setvisibility (view. Gone );
    }

    How can I use it? Run it in the UI thread:

    Myasynctask ynctask = new myasynctask (this, framelayout );
    Ynctask.exe cute (Params );

    The running result is similar to that in the previous section.

    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.