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
Answers
Hi @ValOkafor,
Have you managed to do this or still looking for help?
i want to do the same, how did you got it ?
@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.
@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:Then you have to override
OnActivityResult
method and do something like this:The only thing remains to be mentioned is that
REQUEST_CAMERA
andSELECT_FILE
are my class-level variables:That's it! Hope this helps.
Thanks.
Great! Thanks @astahir , this was very useful!
I got it working on my project
@JorgeWanderSantanaUrena.2443 - Good to know mate...all the best!
Hi @astahir , Thanks for the post, it helped me alot. But is there any way to do the same in Xamarin.Forms?
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!
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:
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