Reputation: 53
I wanted to add camera to my xamarin form app. I watched this video. https://www.youtube.com/watch?v=DJYLrVNY2ak&t=645s
After all the work, I get permission errors.
Error says:
Unhandled Exception: Plugin.Media.Abstractions.MediaPermissionException: Camera permission(s) are required
This code is on MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Plugin.Media.Abstractions;
using Plugin.Media;
using Plugin.Permissions;
using Plugin.Permissions.Abstractions;
namespace camera
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void Button_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable
|| !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("No Camera", "No Camera available", "Ok");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(
new StoreCameraMediaOptions
{
SaveToAlbum = true,
});
if (file == null)
return;
PathLabel.Text = file.AlbumPath;
MainImage.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
}
}
}
And this code is on MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:camera"
x:Class="camera.MainPage">
<StackLayout>
<!-- Place new controls here -->
<Label Text="Welcome to Xamarin.Forms!"
HorizontalOptions="Center"
VerticalOptions="CenterAndExpand" />
<Button Text="Take a Photo"
Clicked="Button_Clicked">
</Button>
<Image x:Name="MainImage"></Image>
<Label x:Name="PathLabel"></Label>
</StackLayout>
</ContentPage>
The emulator open up fine. When I click "Take a Photo" button. I get permission error.
This is error image. Error image
Here is link to file. You can download the file and have a look at the error. All I want this app to do is take some details, take picture and save picture. LINK TO FILE https://contedia-my.sharepoint.com/personal/muhammad_ikram_contedia_com/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fmuhammad%5Fikram%5Fcontedia%5Fcom%2FDocuments%2FPhotos%20%282%29%2Ezip&parent=%2Fpersonal%2Fmuhammad%5Fikram%5Fcontedia%5Fcom%2FDocuments&slrid=6f5b859e-50d6-0000-25d8-0fa7f8d3f9ce
Upvotes: 2
Views: 12526
Reputation: 172
Add this line to your MainActivity in Android
CrossCurrentActivity.Current.Init(this, savedInstanceState);
Add these lines to your code AsseblyInfo
[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
Upvotes: 1
Reputation: 49
Short answer: Check if the permissions are granted to the app from Settings->Apps->YOUR_APP_NAME->Permissions.
Long answer: What I noticed is even using the information from the other answers I still get the same error, so I checked the permissions for the app in Settings->Apps->YOUR_APP_NAME->Permissions and I saw that even though I was given camera access to the app when the pop-up appeared, here I still had no permissions granted to the app. So after I turned the permission on from here everything worked fine.
Upvotes: 0
Reputation: 92
I had also exception Camera permission(s) are required. Try add following code to MainActivity
to OnCreate()
method. It should ask user for camera permission – you should see alert on device
int requestPermissions;
string cameraPermission = Android.Manifest.Permission.Camera;
if (!(ContextCompat.CheckSelfPermission(this, cameraPermission) == (int)Permission.Granted))
{
ActivityCompat.RequestPermissions(this, new String[] { cameraPermission, }, requestPermissions);
}
And put following into android manifest (or check the checkbox)
<uses-permission android:name="android.permission.CAMERA" />
Maybe following link could also be helpful - https://learn.microsoft.com/cs-cz/xamarin/android/app-fundamentals/permissions?tabs=vswin
Upvotes: 0