Introduction
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.
Prerequisites
- 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

Step 4
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.

Main.axml
- <Button android:text="Pick Image" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/MyButton" />
- <ImageView android:src="@android:drawable/ic_menu_gallery" android:layout_width="match_parent" android:layout_height="374.0dp" android:id="@+id/imageView1" />
Next, go to the MainActivity.cs page and write the following code.
MainActivity.cs
- using Android.Net;
- namespace pickimage {
- [Activity(Label = "pickimage", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity: Activity {
- public static readonly int PickImageId = 1000;
- private ImageView _imageView;
- protected override void OnCreate(Bundle bundle) {
- base.OnCreate(bundle);
- // Set our view from the "main" layout resource
- SetContentView(Resource.Layout.Main);
- _imageView = FindViewById < ImageView > (Resource.Id.imageView1);
- Button button = FindViewById < Button > (Resource.Id.MyButton);
- button.Click += ButtonOnClick;
- }
- // Create a Method ButtonOnClick.
- private void ButtonOnClick(object sender, EventArgs eventArgs) {
- Intent = new Intent();
- Intent.SetType("image/*");
- Intent.SetAction(Intent.ActionGetContent);
- StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);
- }
- // Create a Method OnActivityResult(it is select the image controller)
- protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) {
- if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null)) {
- Android.Net.Uri uri = data.Data;
- _imageView.SetImageURI(uri);
- }
- }
- }

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

Output

This shows the location of your images.

Select the Image.

Selected image will be shown successfully..

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

Nwenar IsmailPosted Oct 12, 2021, 12:30 PM
How Can Make My app to allow user to choose a point on the pic as facebook profile chooser?
vishal shetPosted May 25, 2020, 11:39 PM
How to select multiple images ?
Sushil KumarPosted Oct 17, 2019, 2:14 AM
Thanks, Please help me, How to get Image extension type.
ArifPosted Aug 7, 2019, 5:54 AM
Good work man.....may i know why we use the variable public static readonly int PickImageId = 1000; ???
quan tranPosted Jul 12, 2019, 10:14 AM
Hi everybody, when i run program, nothing happen when i pickphoto from gallery??? Help me
Liêm NguyễnPosted Jul 9, 2019, 8:24 PM
How to pick Multi image? Thank!
Sreedevi vPosted Mar 7, 2019, 10:54 PM
How we can add this image picker and button to a content page? plz Help
Shailender Kuma RathorePosted Jul 6, 2018, 1:49 AM
OnActivityResult is not getting called. please help
ali benchardaPosted May 29, 2017, 11:37 AM
How i can pick just a folder
anup kPosted Dec 20, 2016, 6:45 AM
If File Size is greater than 1Mb its now working..!!
Vignesh ManiPosted Oct 21, 2016, 12:31 PM
Nice One