How to give permissions to xamarin camera
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)]
Related videos on Youtube
Tom Wilson
Updated on June 04, 2022Comments
-
Tom Wilson almost 2 years
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
-
Jason over 5 years
-
Tom Wilson over 5 yearsI added this code on the MainActivity.cs. But I still get same permission error.
-
Jason over 5 yearsdo you have Camera enabled in the Android Manifest?
-
Tom Wilson over 5 yearsYes camera checkbox is ticked as well.
-
-
Tom Wilson over 5 yearsI have placed the above code. I get error "Use of unassigned local variable 'requestPermissions'". Does this variable needs to be declared with any value first.
-
Tom Wilson over 5 yearsI added the above code. I saw the alert message first time, I clicked yes but still it threw same permission error
-
Tom Wilson over 5 yearsI have added link where you can download the file look at error.
-
user8013875 over 5 yearsYou saw it only once because now it is enabled (you can see it in your device in details about your app). Not sure but try to add -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
Tom Wilson over 5 yearsStill showing same error. It is only a demo file. If I can share with you; will you have a chance to look at with your eye.
-
Tom Wilson over 5 yearsHere is download link to file. dropbox.com/s/lyv6pz7s0t7be74/camera.sln?dl=0
-
user8013875 over 5 yearsI am not able to access the link, could you post on github?
-
Tom Wilson over 5 yearsHave you tried this link dropbox.com/s/lyv6pz7s0t7be74/camera.sln?dl=0
-
user8013875 over 5 yearsI downloaded solution but i can see no code, it says - unavailable, the project file or web cannot be found
-
Tom Wilson over 5 yearsWould it be possible if I give you old email address than you can email me your email address. Than I can send the zip folder to to your email. Would be really helpful. My email is [email protected]