Download Code

AsynchronousTask(Async_Task)

In this tutorial you will learn another way to download file from server using asynchronoustask(Async_Task) in android application.So,To completely understand this tutorial you have to understand AsynchronousTask(Async_Task) tutorial.

After that,when you have some knowledge of asyncTask.You will be able to get everything what’s on here.

So,let’s start with creating new Android Application Project.

Then go to your project activity_main.xml and use one Button and ImageView.Like this.

async_task,asynchronous task,asynchronoustask,how to download image using asynchronous task in android,how to download image using asynctask tutorial,

After that go to your MainActivity.java class and do the following steps.

  • Create variable of URL.
  • ImageView variable.
  • Button variable.
  • Progress Dialog variable.
  • referring all UI components(Button,ImageView).
  • Apply click event on Button.

Then your MainActivity.java class will look like this.

public class MainActivity extends Activity implements OnClickListener{
	 
		// Set your Image URL into a string
		String URL = "http://www.androidcodec.com/appimages/one.jpg";
		ImageView image;     // imageview variable
		Button downloadBtn;    // image download button
		ProgressDialog mProgressDialog;   // progress dialog variable
	 
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			// Get the layout from image.xml
			setContentView(R.layout.activity_main);
			initialize();    // calling method to set UI components 
		}
	 
		public void initialize(){
			image = (ImageView) findViewById(R.id.image);   // refering id of imageview
			downloadBtn = (Button) findViewById(R.id.dBtn);   // refering id of a button
			downloadBtn.setOnClickListener(this);   // click event on button
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId())
			{
			case R.id.dBtn:		
				break;
			}
		}		
	}

Now,next we will create a child class(ImageDownloader) within the parent class(MainActivity) and import async_Task methods.

public class MainActivity extends Activity implements OnClickListener{
	 
		// Set your Image URL into a string
		String URL = "http://www.androidcodec.com/appimages/one.jpg";
		ImageView image;     // imageview variable
		Button downloadBtn;    // image download button
		ProgressDialog mProgressDialog;   // progress dialog variable
	 
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			// Get the layout from image.xml
			setContentView(R.layout.activity_main);
			initialize();    // calling method to set UI components 
		}
	 
		public void initialize(){
			image = (ImageView) findViewById(R.id.image);   // refering id of imageview
			downloadBtn = (Button) findViewById(R.id.dBtn);   // refering id of a button
			downloadBtn.setOnClickListener(this);   // click event on button
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId())
			{
			case R.id.dBtn:
				
				break;
			}
		}
		// DownloadImage AsynchronousTask
		private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
	 
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				
			}
	 
			@Override
			protected Bitmap doInBackground(String... URL) {
	 
				
			}
	 
			@Override
			protected void onPostExecute(Bitmap result) {
				
				
			}
		}

		
	}

Next what we have to do.First create progress dialog in onPreExecute() method.Then in doInBackground() method connect with the internet and download bytes from server.After that decode that stream,store in Bitmap and return.onPostExecute() will receive that bitmap and set to ImageView.
At last call async_Task in button click case.
MainActivity.java

package androidcodec.aysntaskimageview;


/*
 *
 * All Rights Reserved By: Farhan Rasheed
 * 
 * */



import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;


public class MainActivity extends Activity implements OnClickListener{
	 
		// Set your Image URL into a string
		String URL = "http://www.androidcodec.com/appimages/one.jpg";
		ImageView image;     // imageview variable
		Button downloadBtn;    // image download button
		ProgressDialog mProgressDialog;   // progress dialog variable
	 
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			// Get the layout from image.xml
			setContentView(R.layout.activity_main);
			initialize();    // calling method to set UI components 
		}
	 
		public void initialize(){
			image = (ImageView) findViewById(R.id.image);   // refering id of imageview
			downloadBtn = (Button) findViewById(R.id.dBtn);   // refering id of a button
			downloadBtn.setOnClickListener(this);   // click event on button
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId())
			{
			case R.id.dBtn:
				new ImageDownloader().execute(URL);    // calling asynchronous task
				break;
			}
		}
		// DownloadImage AsyncTask
		private class ImageDownloader extends AsyncTask<String, Void, Bitmap> {
	 
			@Override
			protected void onPreExecute() {
				super.onPreExecute();
				mProgressDialog = new ProgressDialog(MainActivity.this);
				mProgressDialog.setTitle("Downloading Image");   // title of progress dialog
				mProgressDialog.setMessage("Loading...");    // message displaying
				mProgressDialog.setIndeterminate(false);
				mProgressDialog.show();    // show method
			}
	 
			@Override
			protected Bitmap doInBackground(String... URL) {
	 
				String imageURL = URL[0];
	 
				Bitmap bitmap = null;
				try {
					// Download Image from URL
					InputStream input = new java.net.URL(imageURL).openStream();
					// Decode Bitmap  using bitmap factory
					bitmap = BitmapFactory.decodeStream(input);
				} catch (Exception e) {
					e.printStackTrace();
				}
				return bitmap;
			}
	 
			@Override
			protected void onPostExecute(Bitmap result) {
				// Set the bitmap into ImageView
				image.setImageBitmap(result);
				// Close progressdialog
				mProgressDialog.dismiss();
			}
		}

		
	}

Permission of INTERNET should be Android Manifest.xml file.
async_task,asynchronous task,asynchronoustask,how to download image using asynchronous task in android,how to download image using asynctask tutorial,

Now run your android application project.Then your output will look like this.

async_task,asynchronous task,asynchronoustask,how to download image using asynchronous task in android,how to download image using asynctask tutorial,
that’s it for this tutorial.
Thanks.