Firstly, please remove request android permission code (code in #If android ....#end if).
Then uninstall your application in the tested devices or emulators. After that, run FileResult photo = await MediaPicker.Default.CapturePhotoAsync();
, it will popup permission alert, you do not need to write android request runtime permission code.
java.lang.SecurityException camera permission denied on android api 33
Hi, I have deployed an app that's been working fine on android with api < 33, yet, the error "Camera permissions denied" occurs on all devices running api 33. My manifest file is as follows:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.CAMERA" />
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
<uses-sdk android:targetSdkVersion="33" android:minSdkVersion="21" />
This is the code in my code-behind:
private async Task<FileResult> SelectPhotoAsync(Dtos.EvaluationPictureDetailsVM evaluationPicture)
{
#if ANDROID
// this will run for Android 33 and greater
if (DeviceInfo.Platform == DevicePlatform.Android && OperatingSystem.IsAndroidVersionAtLeast(33))
{
var activity = Platform.CurrentActivity ?? throw new NullReferenceException("Current activity is null");
if (ContextCompat.CheckSelfPermission(activity, Manifest.Permission.ReadExternalStorage) != Permission.Granted)
{
ActivityCompat.RequestPermissions(activity, new[] { Manifest.Permission.ReadExternalStorage }, 1);
}
PermissionStatus cameraPermissionsStatus = await Permissions.CheckStatusAsync<Permissions.Camera>(); //=> always returns false
if (cameraPermissionsStatus == PermissionStatus.Denied)
{
bool shouldShowRationale = Permissions.ShouldShowRationale<Permissions.Camera>(); //=> always false
if(shouldShowRationale)
{
await Shell.Current.DisplayAlert("Warning", "Camera permissions are required in order to take a picture", "Ok");
}
cameraPermissionsStatus = await Permissions.RequestAsync<Permissions.Camera>(); //=> now set to true
}
var cameraPermissions = ContextCompat.CheckSelfPermission(activity, Manifest.Permission.Camera); //=> always false
if (cameraPermissions != Permission.Granted)
{
ActivityCompat.RequestPermissions(activity, new[] { Manifest.Permission.Camera }, 1); //=> does it actually do anything? cameraPermissions remains false
}
}
#endif
if (MediaPicker.Default.IsCaptureSupported)
{
if (await MediaPicker.Default.CapturePhotoAsync(opts) is FileResult chosenPhoto) // HERE THE ERROR OCCURS
{
return chosenPhoto;
}
await Shell.Current.DisplayAlert("Error", "Unable to take the picture", "Ok");
}
}
I know Google changed the permissions thing starting from api 33, and, from my understanding, the permissions should not be declared anymore in the manifest file, so I've tried all kind of combinations removing the camera permissions from the manifest file, as well as limiting its application with "maxSdkVersion=32", it just won't work.
Developer technologies .NET .NET MAUI
2 answers
Sort by: Most helpful
-
Anonymous
2024-01-17T09:02:16.4033333+00:00 -
Deleted
This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.
Comments have been turned off. Learn more