Select Image from either the Camera or Gallery

ValOkaforValOkafor USMember, University ✭✭
edited April 2015 in Xamarin.Android

I need to present a Chooser Intent that allows for images to be either selected from the camera or from the gallery. Here is what I have done. First I created a List I called cameraIntent that holds a list of all camera applications on the device.

// Initialize a list to hold any camera application intents.
 var cameraIntents = new List<Intent>();

I used the Package Manager to populate the list and that works, because if I fire up this intent all by itself, it shows a list of all camera apps in the device. I also created another intent that I called galleryIntent that shows a list of the applications in the device that has images like so:

var galleryIntent = new Intent();
galleryIntent.SetType("image/*");
galleryIntent.SetAction(Intent.ActionGetContent);

This also works as expected. What I need now is merge these two Intents and in Java this is how you will accomplish this:

       // Chooser of filesystem options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

        // Start activity to choose or take a picture.
        startActivityForResult(chooserIntent, 1000);

How can I accomplish the above in C#, here is one of my attempts but it is giving error at the line with the Parceable and the array,

             // Chooser of filesystem options.
                 Intent chooserIntent = Intent.CreateChooser(galleryIntent, "Select Source");

                 // Add the camera options.
                 chooserIntent.PutExtra(Intent.ExtraInitialIntents,
                 cameraIntents.ToArray(new Parcelable[cameraIntents.Count()]));

                 // Start activity to choose or take a picture.
                 StartActivityForResult(chooserIntent, 1000);

Any Help is appreciated

Tagged:

Answers

  • astahirastahir USMember ✭✭

    Hi @ValOkafor,

    Have you managed to do this or still looking for help?

  • i want to do the same, how did you got it ?

  • astahirastahir USMember ✭✭

    @JorgeWanderSantanaUrena.2443 - I managed to do it using the following two recipes:

    Take a Picture and Save Using Camera App and Selecting a Gallery Image

    You can browse full code on GitHub (links given in respective recipes) in case you run into any trouble.

  • Thanks @astahir , but in those links i do not see a way to get both features together en only one chooser. They teach it how to do it by separate.

  • Great! Thanks @astahir , this was very useful!
    I got it working on my project

  • astahirastahir USMember ✭✭

    @JorgeWanderSantanaUrena.2443 - Good to know mate...all the best!

  • SVolvoikarSVolvoikar USMember
    edited July 2016

    Hi @astahir , Thanks for the post, it helped me alot. But is there any way to do the same in Xamarin.Forms?

  • astahirastahir USMember ✭✭

    Sorry @SVolvoikar, I won't be able to help you in that as I haven't used Xamarin.Forms

  • Brilliant Astahir, many thanks mate. It works!

  • JohnOkoroaforJohnOkoroafor USMember ✭✭

    @astahir said:
    @JorgeWanderSantanaUrena.2443 - this is how I'm doing:

    Suppose you have a button and on its click you want to show the dialog with options to choose from. On your button's Click, write the following code:

    String[] items = { "Take Photo", "Choose from Library", "Cancel" };
    
    using (var dialogBuilder = new AlertDialog.Builder (this)) {
      dialogBuilder.SetTitle("Add Photo");
      dialogBuilder.SetItems (items, (d, args) => {
          //Take photo
          if (args.Which == 0)
          {
              var intent = new Intent(MediaStore.ActionImageCapture);
              App._file = new File(App._dir, String.Format("karma_{0}.jpg", Guid.NewGuid()));
              intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file));
              this.StartActivityForResult (intent, REQUEST_CAMERA);
          }
          //Choose from gallery
          else if (args.Which == 1)
          {
              var intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
              intent.SetType ("image/*");
              this.StartActivityForResult (Intent.CreateChooser (intent, "Select Picture"), SELECT_FILE);
          }
      });
    
      dialogBuilder.Show ();
    }
    

    Then you have to override OnActivityResult method and do something like this:

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

    if (resultCode == Result.Ok) {
    if (requestCode == REQUEST_CAMERA) {
    // Handle the newly captured image
    } else if (requestCode == SELECT_FILE) {
    // Handle the image chosen from gallery
    }
    }
    }

    The only thing remains to be mentioned is that REQUEST_CAMERA and SELECT_FILE are my class-level variables:

    private static readonly Int32 REQUEST_CAMERA = 0;
    private static readonly Int32 SELECT_FILE = 1;
    

    That's it! Hope this helps.

    Thanks.

    Ho do you handle the image? This post is somewhat dead but I need to know? Is it stored in a variable or what?

  • DronisheDronishe USMember

    Ho do you handle the image? This post is somewhat dead but I need to know? Is it stored in a variable or what?

    And how do you whant to handle it?

    You can show it in your app simply like this:

                    if (requestCode == SELECT_FILE)
                    {
                        var imgButton = FindViewById<ImageButton>(Resource.Id.btnAddPhoto);
                        imgButton.SetImageURI(data.Data);
                    }
    
  • mariuspavelmariuspavel Member ✭✭

    I tried the solution above and it seems to work, but the problem that I still have here is that if I click on cancel or anywhere around the popup (AlertDialog) the file picker button doesn't respond anymore on click. Actually If I click for the first time the file picker button, the AlertDialog is opening and I'm able to choose between "Gallery", "Camera" and "Cancel". If I click cancel the file picker is not opening for the second time. The same scenario is happening if I close the AlertDialog by closing somewhere outside of it.

    Do have any suggestion on this side? Is there something that I missed?

    Thank you

Sign In or Register to comment.