Is anyone able to give a short, self-contained example on how to access the camera with Xamarin.Forms 1.3.x? Simply calling the native camera application and retrieving the resulting picture would be great. Displaying a live view on the Xamarin.Forms page would be awesome!
I already tried to use Xamarin.Mobile and Xamarin.Forms.Labs, but I couldn't get any solution to work on both platforms (focussing on Android and iOS for now). Most code snippets found on the web (including stackoverflow) are incomplete, e.g. not showing the implementation of an IMediaPicker object or where to anchor the method for taking pictures.
I finally created a minimum solution for iOS and Android.
The shared project
First, let's look into the shared code. For an easy interaction between the shared
App
class and the platform-specific code we store a staticInstance
within thepublic static App
:Furthermore, we will display an
Image
, which will be filled with content later. So we create a member:Within the
App
constructor we store theInstance
and create the page content, which is a simplebutton
and the aforementionedimage
:The button's click handler calls the event
ShouldTakePicture
. It is a public member and the platform-specific code parts will assign to it later on.Finally, we offer a public method for displaying the captured image:
The Android project
On Android we modify the
MainActivity
. First, we define a path for the captured image file:At the end of
OnCreate
we can use the staticInstance
of the createdApp
and assign an anonymous event handler, which will start a newIntent
for capturing an image:Last but not least, our activity has to react on the resulting image. It will simply push its file path to the shared
ShowImage
method.That's about it! Just don't forget to set the "Camera" and the "WriteExternalStorage" permission within "AndroidManifest.xml"!
The iOS project
For the iOS implementation we create a custom renderer. Therefore, we add a new file "CustomContentPageRenderer" and add the corresponding assembly attribute right after the using statements:
The
CustomContentPageRenderer
inherits fromPageRenderer
:We override the
ViewDidAppear
method and add the following parts.Create a new image picker controller referring to the camera:
Present the image picker controller, as soon as the
ShouldTakePicture
event is raised:After taking the picture, save it to the
MyDocuments
folder and call the sharedShowImage
method:And finally, we need to handle a cancellation of the image taking process:
Try out James Montemagno's MediaPlugin.
You can install the plugin using Package Manager Console by simply typing and running
Install-Package Xam.Plugin.Media -Version 2.6.2
or else go to Manage NuGet Packages... and typeXam.Plugin.Media
and install the plugin. (The plugins has to be installed in all your projects - including the client projects)A readme.txt will be prompted and follow the instructions there. After that, add the following codes (as required) to your shared project. The instructions to be followed in the above readme.txt file are as follows.
For Android Project
In your BaseActivity or MainActivity (for Xamarin.Forms) add this code:
You must also add a few additional configuration files to adhere to the new strict mode:
Add the following to your AndroidManifest.xml inside the <application> tags:
YOUR_APP_PACKAGE_NAME must be set to your app package name!
Add a new folder called xml into your Resources folder and add a new XML file called
file_paths.xml
Add the following code:
YOUR_APP_PACKAGE_NAME must be set to your app package name!
For iOS Project
Your app is required to have keys in your Info.plist for
NSCameraUsageDescription
andNSPhotoLibraryUsageDescription
in order to access the device's camera and photo/video library. If you are using the Video capabilities of the library then you must also addNSMicrophoneUsageDescription
. The string that you provide for each of these keys will be displayed to the user when they are prompted to provide permission to access these device features.Such as:
For Shared Project
To simply open camera, save photo and display an alert with the file path, enter the following to the shared project.
Here is how you can do it on Xamarin Forms cross Xamarin iOS.
This is a good base to start but it requires a Page to rendered first in which you could just specify the UIApplication for it to provide the UIView for Camera / Photo Picker controllers.
https://stackoverflow.com/a/28299259/1941942
Portable Project
iOS Project
Here's what I needed to get async camera-capture running in my app:
In iOS:
And if you need the authorization routine, make sure you also fill out the camera use in
info.plist
too, and here's the function to get authorization:In Android: