Download Code

Xamarin ImageView Example Android

In this tutorial you will learn about ImageView,Camera and how to combine these two things to build Xamarin ImageView example android Wallpaper App.

So i’ll give you some information about ImageView and Camera first.

Xamarin ImageView

Image View displays an arbitrary image or icon.Image View is basically used to display captured image from camera in an application like Retrica.

Camera

The android framework provides different support of cameras and camera according to the devices.Camera is basically used to captured image and video.You can read further detail about Camera from Android Developer Official Website.

Let’s start by creating new android application project.File > New > Solution > Android App.I named it “Camera”.

First go to your Main.axml file.Add axml code or drag & drop button,ImageView.

Xamarin imageview,Xamarin imageview example,xamarin imageview example android,imageview example android,wallpaper app using xamarin,

Next create new class Resources > Add > New File > Activity (BitmapHelper.cs).This class is basically used to resize image.

Main Components of this class.

  • Bitmap
  • BitmapFactory

BitmapHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace camera
{
	using System.IO;

	using Android.Graphics;

	public static class BitmapHelpers
	{
		public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height)
		{
			// First we get the the dimensions of the file on disk
			BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true };
			BitmapFactory.DecodeFile(fileName, options);

			// Next we calculate the ratio that we need to resize the image by
			// in order to fit the requested dimensions.
			int outHeight = options.OutHeight;
			int outWidth = options.OutWidth;
			int inSampleSize = 1;

			if (outHeight > height || outWidth > width)
			{
				inSampleSize = outWidth > outHeight
					? outHeight / height
					: outWidth / width;
			}

			// Now we will load the image and have BitmapFactory resize it for us.
			options.InSampleSize = inSampleSize;
			options.InJustDecodeBounds = false;
			Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options);

			return resizedBitmap;
		}
	}
}

Now go to MainActivity.cs class.Create variables for imageview,button,file,file_directory,bitmap.The create some methods to start camera using intent.

MainActivity.cs

using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Provider;
using Android.Widget;
using Java.IO;
using Environment = Android.OS.Environment;
using Uri = Android.Net.Uri;

namespace camera
{
	

	[Activity(Label = "camera", MainLauncher = true)]
	public class MainActivity : Activity
	{
		private ImageView _imageView;   // image view variable
		WallpaperManager myWall;        // wallpaper manager variable
		public static File _file;       // File class variable
		public static File _dir;        // File class variable will be used for directory
		public static Bitmap bitmap;    // Bitmap class variable

		protected override void OnCreate(Bundle bundle)
		{
			base.OnCreate(bundle);
			SetContentView(Resource.Layout.Main);
			myWall = WallpaperManager.GetInstance(this);
			if (IsThereAnAppToTakePictures())
			{
				CreateDirectoryForPictures();

				Button button = FindViewById< Button >(Resource.Id.myButton);
				_imageView = FindViewById(Resource.Id.imageView1);
				button.Click += TakeAPicture;
			}

		}
		private void TakeAPicture(object sender, EventArgs eventArgs)
		{
			Intent intent = new Intent(MediaStore.ActionImageCapture);

			_file = new File(_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));

			intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));

			StartActivityForResult(intent, 0);
		}

		private void CreateDirectoryForPictures()
		{
			_dir = new File(
				Environment.GetExternalStoragePublicDirectory(
					Environment.DirectoryPictures), "CameraAppDemo");
			if (!_dir.Exists())
			{
				_dir.Mkdirs();
			}
		}

		private bool IsThereAnAppToTakePictures()
		{
			Intent intent = new Intent(MediaStore.ActionImageCapture);
			IList availableActivities = 
				PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
			return availableActivities != null && availableActivities.Count > 0;
		}


		protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
		{
			base.OnActivityResult(requestCode, resultCode, data);

			// Make it available in the gallery

			Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
			Uri contentUri = Uri.FromFile(_file);
			mediaScanIntent.SetData(contentUri);
			SendBroadcast(mediaScanIntent);

			// Display in ImageView. We will resize the bitmap to fit the display
			// Loading the full sized image will consume to much memory 
			// and cause the application to crash.

			int height = Resources.DisplayMetrics.HeightPixels;
			int width = _imageView.Height ;
			bitmap = _file.Path.LoadAndResizeBitmap (width, height);
			if (bitmap != null) {
				_imageView.SetImageBitmap (bitmap);
				myWall.SetBitmap (bitmap);
				bitmap = null;
			}

			// Dispose of the Java side bitmap.
			GC.Collect();
		}





	}
}

Go to your Project Properties > AndroidManifest.xml file.Add permissios for.

  • Write External Storage
  • Camera
  • Set Wallpaper

Xamarin imageview,Xamarin imageview example,xamarin imageview example android,imageview example android,wallpaper app using xamarin,

 

Now Run your xamarin imageview example android application project.Your output should be.

Xamarin imageview,Xamarin imageview example,xamarin imageview example android,imageview example android,wallpaper app using xamarin,

That’s it for xamarin imageview example android wallpaper app tutorial.I hope you got the idea how to use camera.

Thanks