Download Code


In this tutorial you will learn another way to download file from server using asynchronoustask in android application.So,To completely understand this tutorial you have to understand AsynchronousTask 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 Main.axml and use one Button and ImageView.

asynchronoustask,asynctask xamarin,asynctask in android xamarin,C# xamarin,download image,xamarin android

After that go to your MainActivity.cs 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.
  • Create a child class(ImageDownloader) within the parent class(MainActivity) and import asyncTask methods.

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.

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
using System;
using Android.Graphics;
using System.Net;

namespace XamarinImageAsyncTask
	[Activity(Label = "XamarinImageAsyncTask", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity,Android.Views.View.IOnClickListener
		// Set your Image URL into a string
		string URL = "";
		ImageView image;     // imageview variable
		Button downloadBtn;    // image download button
		ProgressDialog mProgressDialog;   // progress dialog variable

		protected override void OnCreate(Bundle savedInstanceState)

			// Set our view from the "main" layout resource

		public void initialize()
			image = (ImageView)FindViewById(Resource.Id.image);
			downloadBtn = ( Button )FindViewById(Resource.Id.dBtn);

		public void OnClick(View v)
			switch (v.Id)
				case Resource.Id.dBtn:
					ImageDownloader ID = new ImageDownloader(this);

		private class ImageDownloader : AsyncTask<String, Java.Lang.Object, Bitmap>
			MainActivity mainActivity;

			public ImageDownloader(MainActivity mainActivity)
				this.mainActivity = mainActivity;

			protected override void OnPreExecute()
				mainActivity.mProgressDialog = new ProgressDialog(mainActivity);
				mainActivity.mProgressDialog.SetTitle("Downloading Image");

			protected override Bitmap RunInBackground(params string[] @params)
				string imageURL = @params[0];
				Bitmap myBitmap = null;
				using ( var webClient = new WebClient())
					var bytes = webClient.DownloadData(imageURL);
					if (bytes != null && bytes.Length > 0)
						myBitmap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length);

				return myBitmap;
			protected override void OnPostExecute(Bitmap result)



Permission of INTERNET Android Manifest.xml file.

asynchronous task,asynctask xamarin,asynctask in android xamarin,C# xamarin,download image,xamarin android

Now run your android application project.

asynchronoustask,asynctask xamarin,asynctask in android xamarin,C# xamarin,download image,xamarin android

That’s it for this tutorial.For Java example you can visit Android Codec.