Forum Xamarin Xamarin.Forms

xamarin.forms android camera permissoins denied

erwinnerwinn Member
edited April 2018 in Xamarin.Forms

Hi,

I am trying to take a photo with a tablet and keep getting 'permission denied'.
What did i do wrong?

camara status and storage status are both 'denied'.

private async void BtnTakePhoto_Clicked(object sender, EventArgs e)
{
await CrossMedia.Current.Initialize();

        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await DisplayAlert("No Camera", "No camera avaialble.", "OK");
            return;
        }

        try
        {
            var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
            var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

            if (cameraStatus != PermissionStatus.Granted || storageStatus != PermissionStatus.Granted)
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage });
                cameraStatus = results[Permission.Camera];
                storageStatus = results[Permission.Storage];
            }

            if (cameraStatus == PermissionStatus.Granted && storageStatus == PermissionStatus.Granted)
            {
                var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    Directory = "Sample",
                    Name = "test.jpg"
                });
            }
            else
            {
                await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK");

                CrossPermissions.Current.OpenAppSettings();
            }
        }
        catch(Exception error)
        {
            await DisplayAlert("Alert!", error.ToString(), "OK");
            throw error;
        }

}

android manifest:

<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="Xamarin">
<provider android:name="android.support.v4.content.FileProvider"
                  android:authorities="com.test.Xamarin.fileprovider"
                android:exported="false"
                  android:grantUriPermissions="true">
  <meta-data android:name="android.support.FILE_PROVIDER_PATHS"
                     android:resource="@xml/file_paths"></meta-data>
</provider>    
</application>

mainacticity:

namespace XMLSample.Droid
{
[Activity(Label = "test", MainLauncher = true, ScreenOrientation = creenOrientation.Landscape)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override async void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        await CrossMedia.Current.Initialize();

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
    {
        PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

}

assemblyinfo

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
[assembly: UsesPermission(Android.Manifest.Permission.Camera)]

[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]

Answers

Sign In or Register to comment.