Selecting a Gallery Image

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


This recipe shows how to read an image from the gallery and display it in an ImageView.


  1. Add an ImageView to Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:text="Pick an image from the gallery"/>

  1. In an Activity subclass, create an Intent with a MIME type set to “image/*” and an action set to ActionGetContent. Pass the intent to a StartActivityForResult method call.
button.Click += delegate {
    var imageIntent = new Intent ();
    imageIntent.SetType ("image/*");
    imageIntent.SetAction (Intent.ActionGetContent);
    StartActivityForResult (
        Intent.CreateChooser (imageIntent, "Select photo"), 0);
  1. Override OnActivityResult and set the image Uri of the ImageView to the Uri of the selected image.
protected override void OnActivityResult (int requestCode, Result resultCode, Intent data)
    base.OnActivityResult (requestCode, resultCode, data);

    if (resultCode == Result.Ok) {
        var imageView =
            FindViewById<ImageView> (Resource.Id.myImageView);
        imageView.SetImageURI (data.Data);

Note that no image is shown initially until it is selected from the gallery. Once selected, the image appears on the screen as shown above.

Additional Information

The Data property of the Intent returned to OnActivityResult will contain the Uri of the selected image. We check the result in case the user cancelled the selection.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.