Skip to content
Branch: master
Find file History
Latest commit d0ef8a5 Mar 12, 2018

id title subtitle brief api dateupdated
Select An Image
How to Pick an Image
This recipe will show how to pick an image that is stored on the device and display it in an ImageView widget.

This recipe will show how to pick a image stored on the device, display it in an ImageView and obtain the path the file on the device. This recipe accomplishes this by firing off an Intent to start an application (of the users choice) to select the image. The selection will be sent back to our application via a URI that is wrapped in an Intent. The following screenshots shows the sample application included with this recipe.


  1. Create a new Android Application in Visual Studio for Mac or Visual Studio.
  2. Add a Button and an ImageView to Main.axml, as shown in the following XML:
``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/MyButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Pick Image" /> <ImageView android:src="@android:drawable/ic_menu_gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView1" /> </LinearLayout> ```
  1. Add the following instance variables to Activity:
[Activity(Label = "PickImageFromGallery", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
    public static readonly int PickImageId = 1000;
    private ImageView _imageView;
  1. Change the code in OnCreate as shown in the following snippet:
protected override void OnCreate(Bundle bundle)

    _imageView = FindViewById&lt;ImageView&gt;(Resource.Id.imageView1);
    Button button = FindViewById&lt;Button&gt;(Resource.Id.MyButton);
    button.Click += ButtonOnClick;
  1. Create the ButtonOnClick event handler with the following method:
private void ButtonOnClick(object sender, EventArgs eventArgs)
    Intent = new Intent();
    StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);

This event handler will fire off an Intent to Android. The intent specifies two things:

  • An Action - The code above sets the action to Intent.ActionGetCode. This is the default action.
  • Content Type - this is set to the MIME type for all images.

The helper method Intent.CreateChooser will present a dialog to the user with a list of all applications that can handle the intent, as shown in this screenshot:

Once the user selects their image, Intent.CreateChooser will create an Intent that will be used by StartActivityForResult to start the application to handle the request. When that application is finished, Android will invoke the OnActivityResult method in our application.

  1. To handle the request for the image picking application, we need to override the method OnActivityResult with the following code:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
        Uri uri = data.Data;

In this method we ensure that we only respond to the request that our Activity originally sent out in the the ButtonOnClick event handler. We will obtain the Uri of the selected image and display that image in the ImageView.

  1. Run the application, and you should get screens similar to the following:
![](Images/image01.png) ![](Images/image02.png) ![](Images/image03.png) ![](Images/image04.png)
You can’t perform that action at this time.