(Android) Download Images by AsyncTask API

Source: Internet
Author: User

1. Check network status

AndroidManifest. xml

<Uses-sdk>... </>

<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>

<Application>

 

MainActivity. java

Public boolean isNetworkAvailable (){
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo activeNetworkInfo = connectivityManager. getActiveNetworkInfo ();
Return activeNetworkInfo! = Null & activeNetworkInfo. isConnected ();
}

 

 

 

2. Extends AsyncTask to download images.

Normally, we create mutl-thread which is used to download images.

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread andHandler and does not constitute a generic threading framework. asyncTasks shocould ideally be used for short operations (a few seconds at the most .) if you need to keep threads running for long periods of time, it is highly recommended you use the varous APIs provided by thejava. util. concurrent pacakge such as Executor, ThreadPoolExecutor andFutureTask.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. an asynchronous task is defined by 3 generic types, calledParams, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate andonPostExecute.

Public class ImageDownload extends AsyncTask <String, Integer, Boolean> {

Private ImageView view;
Private Bitmap img;

Public ImageDownload (ImageView view ){
This. view = view;
}

@ Override
Protected Boolean doInBackground (String... urls ){
Try {
URL imageUrl = new URL (urls [0]);
URLConnection conn = imageUrl. openConnection ();
Img = BitmapFactory. decodeStream (conn. getInputStream ());

} Catch (MalformedURLException e ){
E. printStackTrace ();
Return false;
} Catch (IOException e ){
E. printStackTrace ();
Return false;
}
Return true;
}

@ Override
Protected void onPostExecute (Boolean downloadResult ){
If (downloadResult ){
View. setImageBitmap (img );
}
}
 
Protected void onProgressUpdate (Integer... progress ){
// Set progress
}

}

 

 

3. Begin to download images

String [] urls = new String [1];
Urls [0] = "http://media.zenfs.com/ko_KR/News/starnn/20130121130545_50fcbe998c3d0_1.jpg ";
ImageDownload imageDownload = new ImageDownload (imageDownloadView );
ImageDownload.exe cute (urls );

 

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.