There are two ways to implement the asynchronous task mechanism in Android, Handler and AsyncTask. This article introduces the function of Android using AsyncTask to download pictures and display progress bars.
AsyncTask downloads pictures and displays the download progress. AsyncTask is a synchronous class that works with the progress bar, concise!
public class AsyncTaskActivity2 extends Activity { private Button btnDown;//Picture frame private ImageView ivImage;//Picture URL private static String image_path = "/page/main1406/images/"; //Progress dialog private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.asynctask_activity); btnDown = (Button) findViewById(); ivImage = (ImageView) findViewById(); // Create a new dialog box and will not be displayed dialog = new ProgressDialog(this); ("hint"); ("Downloading, please wait..."); (ProgressDialog.STYLE_HORIZONTAL);//With horizontal scroll bar (false);//cannot // After clicking the button, execute AsyncTask and pass in the image URL (new () { @Override public void onClick(View v) { // Execute asynchronous tasks new MyTask().execute(image_path);//Start AsyncTask, the parameter type is the type of the first parameter of the asynchronous class } }); } //Create an internal class to download pictures public class MyTask extends AsyncTask<String, Integer, Bitmap> { @Override protected void onPreExecute() {//Open the asynchronous class first after starting it, you can set it (); ();//Show dialog box } //Step 2, generally complicated processing @Override protected Bitmap doInBackground(String... params) {//The most important thing is to process complexly, backend, receive parameters transmitted from execute Bitmap bitmap = null; //Binary pictures //Byte array output stream ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = null;//Byte input stream try { HttpClient httpClient = new DefaultHttpClient();//Create an Http client HttpGet httpGet = new HttpGet(params[0]);//Send a request HttpResponse httpResponse = (httpGet);//return if (().getStatusCode() == 200) {//success inputStream = ().getContent();//Get the return content as input stream long file_length = ().getContentLength();//File length int len = 0; byte[] data = new byte[1024];//Bytes read each time int total_length = 0; // Read image data in bytes while ((len = (data)) != -1) { total_length += len; // Calculate progress int values = (int) ((total_length / (float) file_length) * 100); // Release progress information publishProgress(values);//Release progress, AsyncTask's second parameter type triggers onProgressUpdate update progress bar (data, 0, len);//Write output stream } byte[] result=();//Convert the byte array output stream to byte array //Generate binary pictures bitmap=(result, 0, ); } } catch (Exception e) { (); } finally { try { if (inputStream != null) { (); } } catch (Exception e2) { } } return bitmap; } // When there is data sent by publishProgress(value) triggered, update the UI @Override protected void onProgressUpdate(Integer... values) { (values); // Set the progress value of the progress dialog box (values[0]);//Update progress bar, run in UI } //Step 4 @Override protected void onPostExecute(Bitmap result) {//Hide the dialog box after downloading (result); (); (result); //Update the UI, display the picture, run in the UI } } }
The above is the function of Android using AsyncTask to download pictures and display progress bars. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!