I am currently going through the process of Learning Xamarin.Forms. I am currently attempting to implement Camera functions using the Plugin.Media.CrossMedia library.
Implemented below:
public async Task<ImageSource> StartCamera()
{
await CrossMedia.Current.Initialize();
if (Plugin.Media.CrossMedia.Current.IsTakePhotoSupported && CrossMedia.Current.IsCameraAvailable)
{
var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions() { SaveToAlbum=false, SaveMetaData=false});
if (photo != null)
return ImageSource.FromStream(() => { return photo.GetStream(); });
else
return null;
}
else
{
return null;
}
}
However upon executing the 'TakePhotoAsync' method I recieve the following error.
System.ArgumentException: Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Please check documentation on how to set this up in your project.
I have tried looking this up but to no avail. My AndroidManifest.xml file looks like this at the moment.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="RCPTTracker.Android" android:versionCode="1" android:versionName="1.0">
<uses-sdk />
<application android:label="RCPTTracker.Android"></application>
</manifest>
My inkling is that I need to declare something in here, but I haven't been able to find what, and all tutorials i have followed don't go through anything with setting up a File Provider.
Any suggestions?
The readme says:
If your application targets Android N (API 24) or newer, you must use version 2.6.0+.
You must also add a few additional configuration files to adhere to the new strict mode:
1.) Add the following to your AndroidManifest.xml inside the tags:
<provider android:name="android.support.v4.content.FileProvider"
android:authorities="YOUR_APP_PACKAGE_NAME.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
YOUR_APP_PACKAGE_NAME must be set to your app package name!
2.) Add a new folder called xml into your Resources folder and add a new XML file called file_paths.xml
Add the following code:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>
YOUR_APP_PACKAGE_NAME must be set to your app package name!
You can read more at: https://developer.android.com/training/camera/photobasics.html