How To Pick An Image From Gallery In Xamarin Android App


Xamarin is a platform to develop cross-platform and multi-platform apps (Ex. Windows phone, Android, iOS). In Xamarin, the code sharing concept is used. The Xamarin Studio is available in Visual Studio also.


  • Visual Studio 2015 update 3

The following steps are needed to be followed in order to pick an image from Gallery in an Android app.

Step 1

Open Visual Studio and go to File --> New--> Project, or click (Ctrl+Shift+N).

Step 2

Select Installed --> Templates --> Visual C# --> Android --> choose the Blank App (Android).

Next, give your Android app a name (Ex:sample) and give a path to your project. Finally, click OK.

Step 3

Go to the Solution Explorer and select Resource --> Layout --> double click to open Main.axml page. You can select either the code view or the designer view, based on your expertise.

Step 4
I have opened the page in designer view. Now, delete the default "hello world" button by going to the source panel and deleting its coding.

Also, go to the MainActivity.cs page and delete the C# button action code.

Step 5

Next, go to the toolbox window and scroll down. Spot the Button control and drag and drop it on the page design.

Step 6

Drag and drop the Image View.

Step 7

Next, go to the properties window and edit the Button text value and id value(Ex: android:text="Pick Image" android:id="@+id/MyButton" ).

Step 8

Edit the Image View properties or note the values.

  1. <Button android:text="Pick Image" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/MyButton" />  
  2. <ImageView android:src="@android:drawable/ic_menu_gallery" android:layout_width="match_parent" android:layout_height="374.0dp" android:id="@+id/imageView1" />  
Step 9

Next, go to the MainActivity.cs page and write the following code.

  1. using Android.Net;  
  2. namespace pickimage {  
  3.     [Activity(Label = "pickimage", MainLauncher = true, Icon = "@drawable/icon")]  
  4.     public class MainActivity: Activity {  
  5.         public static readonly int PickImageId = 1000;  
  6.         private ImageView _imageView;  
  7.         protected override void OnCreate(Bundle bundle) {  
  8.                 base.OnCreate(bundle);  
  9.                 // Set our view from the "main" layout resource   
  10.                 SetContentView(Resource.Layout.Main);  
  11.                 _imageView = FindViewById < ImageView > (Resource.Id.imageView1);  
  12.                 Button button = FindViewById < Button > (Resource.Id.MyButton);  
  13.                 button.Click += ButtonOnClick;  
  14.             }  
  15.             // Create a Method ButtonOnClick.   
  16.         private void ButtonOnClick(object sender, EventArgs eventArgs) {  
  17.                 Intent = new Intent();  
  18.                 Intent.SetType("image/*");  
  19.                 Intent.SetAction(Intent.ActionGetContent);  
  20.                 StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);  
  21.             }  
  22.             // Create a Method OnActivityResult(it is select the image controller)   
  23.         protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) {  
  24.             if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null)) {  
  25.                 Android.Net.Uri uri = data.Data;  
  26.                 _imageView.SetImageURI(uri);  
  27.             }  
  28.         }  
  29.     }  

Step 10

If you have Android Virtual Device, run your app on it. Else, run it on your Android phone.
Simply, connect your Android phone to the system via USB cable. When you open the Visual Studio, the Android phone name is shown in the connected devices of Run menu (Ex: LENOVO A6020a40 (Android 5.1-API 22)). 

Next, click on the connected Android phone. It will build and run the app in your device.

After a few seconds, the app will start running in your phone, as shown below.
Click the "Pick Image" button.

This shows the location of your images.

Select the Image.

Selected image will be shown successfully..


Thus, this was the process of picking an image from Gallery in Android phone, using Visual Studio 2015 update 3.

Similar Articles