[Bug] Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread. MediaPicker
Description
I try to use the Phone Camera in my application. I followed the instructions to add MediaPicker according to Microsoft Documentation, but when the app is running on the Android Simulator throws me Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread. I'm using F#
Steps to Reproduce
- Add Xamarin Essentials version: 1.6.0-ci.2 to the project

- Add Permissions in the Android Manifest Files.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="17" android:targetSdkVersion="29" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="ColPatApp.Android"></application>
</manifest>
- Add the MainActivity overrides to run the permissions on the main thread
[<Activity (Label = "ColPatApp.Android", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = (ConfigChanges.ScreenSize ||| ConfigChanges.Orientation))>]
type MainActivity() =
inherit FormsAppCompatActivity()
override this.OnCreate (bundle: Bundle) =
FormsAppCompatActivity.TabLayoutResource <- Resources.Layout.Tabbar
FormsAppCompatActivity.ToolbarResource <- Resources.Layout.Toolbar
base.OnCreate (bundle)
Xamarin.Essentials.Platform.Init(this, bundle)
Xamarin.Forms.Forms.Init (this, bundle)
let appcore = new ColPatApp.App()
this.LoadApplication (appcore)
override this.OnRequestPermissionsResult(requestCode: int, permissions: string[], [<GeneratedEnum>] grantResults: Android.Content.PM.Permission[]) =
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults)
base.OnRequestPermissionsResult(requestCode, permissions, grantResults)
- Add a Button code to take a photo

- Add the code (Elmish Model) to a function and run it synchronously
let takePicture =
async{
printfn "Inside Function"
let! perCamStat = Permissions.CheckStatusAsync<Permissions.Camera>() |> Async.AwaitTask
if perCamStat = PermissionStatus.Granted then
let! getPhoto = MediaPicker.CapturePhotoAsync(options = null) |> Async.AwaitTask
let! stream = getPhoto.OpenReadAsync() |> Async.AwaitTask
let img = ImageSource.FromStream(fun _ -> stream)
printfn "Foto Capturada Exitosamente"
else
let! perCamReq = Permissions.RequestAsync<Permissions.Camera>() |> Async.AwaitTask
if perCamReq = PermissionStatus.Granted then
let! getPhoto = MediaPicker.CapturePhotoAsync(options = null) |> Async.AwaitTask
let! stream = getPhoto.OpenReadAsync() |> Async.AwaitTask
let img = ImageSource.FromStream(fun _ -> stream)
printfn "Foto Capturada Exitosamente"
else
printfn "Permiso no concedido"
}
let update msg model =
match msg with
| SendPatente -> model, Cmd.none
| PatenteChanged text -> { model with Patente = text}, Cmd.none
| DescriptionChanged text -> { model with Description = text}, Cmd.none
| GetCurrentPos -> let pos = Async.RunSynchronously getCoordinates
{ model with Latitud = pos.Latitude; Longitud = pos.Longitude}, Cmd.none
| TakePhoto -> Async.RunSynchronously takePicture
model , Cmd.none
View.Button(text = "Camara", command = (fun _ -> dispatch TakePhoto ))
Expected Behavior
Raise the Camera to simulate a photo capture.
Actual Behavior
The Following Exception is thrown
10-20 21:14:01.285 I/mono-stdout(15168): Message: TakePhoto
Thread started: <Thread Pool> #2
Thread started: <Thread Pool> #3
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Inside Function
10-20 21:14:01.519 I/mono-stdout(15168): Inside Function
10-20 21:14:01.558 D/Mono (15168): Loading reference 5 of /storage/emulated/0/Android/data/com.companyname/files/.__override__/Xamarin.Essentials.dll asmctx DEFAULT, looking for System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
10-20 21:14:01.559 D/Mono (15168): Assembly Ref addref Xamarin.Essentials[0xecb489a0] -> System.Core[0xe5eeed20]: 6
10-20 21:14:01.583 D/Mono (15168): DllImport searching in: '__Internal' ('(null)').
10-20 21:14:01.584 D/Mono (15168): Searching for 'java_interop_jnienv_get_object_field'.
10-20 21:14:01.584 D/Mono (15168): Probing 'java_interop_jnienv_get_object_field'.
10-20 21:14:01.584 D/Mono (15168): Found as 'java_interop_jnienv_get_object_field'.
10-20 21:14:03.430 I/mono-stdout(15168): Error in model function: System.AggregateException: One or more errors occurred. (Permission request must be invoked on main thread.) ---> Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
10-20 21:14:03.430 I/mono-stdout(15168): at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116
10-20 21:14:03.430 I/mono-stdout(15168): --- End of inner exception stack trace ---
10-20 21:14:03.430 I/mono-stdout(15168): at Microsoft.FSharp.Control.AsyncResult`1[T].Commit () [0x0002c] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
10-20 21:14:03.431 I/mono-stdout(15168): at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronouslyInAnotherThread[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000a3] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
Error in model function: System.AggregateException: One or more errors occurred. (Permission request must be invoked on main thread.) ---> Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116
--- End of inner exception stack trace ---
at Microsoft.FSharp.Control.AsyncResult`1[T].Commit () [0x0002c] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronouslyInAnotherThread[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000a3] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T] (System.Threading.CancellationToken cancellationToken, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpO10-20 21:14:03.431 I/mono-stdout(15168): at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T] (System.Threading.CancellationToken cancellationToken, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x00007] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
ption`1[T] timeout) [0x00007] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x0006e] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at ColPatApp.AppModule.update[a] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00119] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:79
at [email protected] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00000] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:99
at Fabulous.ProgramModule+withConsoleTrace@203-1[model,msg,arg].Invoke (msg msg, model model) [0x0001b] in <431f22c722e5131029cce0ef5b1b28fe>:0
---> (Inner Exception #0) Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
at Xamarin.Ess
entials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116 <---
10-20 21:14:03.431 I/mono-stdout(15168): at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x0006e] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
10-20 21:14:03.431 I/mono-stdout(15168): at ColPatApp.AppModule.update[a] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00119] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:79
10-20 21:14:03.431 I/mono-stdout(15168): at [email protected] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00000] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:99
10-20 21:14:03.431 I/mono-stdout(15168): at Fabulous.ProgramModule+withConsoleTrace@203-1[model,msg,arg].Invoke (msg msg, model model) [0x0001b] in <431f22c722e5131029cce0ef5b1b28fe>:0
10-20 21:14:03.432 I/mono-stdout(15168): ---> (Inner Exception #0) Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
10-20 21:14:03.432 I/mono-stdout(15168): at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116 <---
10-20 21:14:03.432 I/mono-stdout(15168):
Unable to process a message:: System.AggregateException: One or more errors occurred. (Permission request must be invoked on main thread.) ---> Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116
--- End of inner exception stack trace ---
at Microsoft.FSharp.Control.AsyncResult`1[T].Commit () [0x0002c] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronouslyInAnotherThread[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000a3] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
10-20 21:14:03.478 I/mono-stdout(15168): Unable to process a message:: System.AggregateException: One or more errors occurred. (Permission request must be invoked on main thread.) ---> Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
10-20 21:14:03.479 I/mono-stdout(15168): at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116
10-20 21:14:03.479 I/mono-stdout(15168): --- End of inner exception stack trace ---
at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T] (System.Threading.CancellationToken cancellationToken, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x00007] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x0006e] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
at ColPatApp.AppModule.update[a] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00119] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:79
at [email protected] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00000] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:99
at Fabulous.ProgramModule+withConsoleTrace@203-1[model,msg,arg].Invoke (m10-20 21:14:03.479 I/mono-stdout(15168): at Microsoft.FSharp.Control.AsyncResult`1[T].Commit () [0x0002c] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
sg msg, model model) [0x00082] in <431f22c722e5131029cce0ef5b1b28fe>:0
at Fabulous.ProgramRunner`3[arg,model,msg].processMsg (msg msg) [0x00000] in <431f22c722e5131029cce0ef5b1b28fe>:0
---> (Inner Exception #0) Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116 <---
10-20 21:14:03.479 I/mono-stdout(15168): at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronouslyInAnotherThread[a] (System.Threading.CancellationToken token, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x000a3] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
10-20 21:14:03.479 I/mono-stdout(15168): at Microsoft.FSharp.Control.AsyncPrimitives.RunSynchronously[T] (System.Threading.CancellationToken cancellationToken, Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout) [0x00007] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
10-20 21:14:03.480 I/mono-stdout(15168): at Microsoft.FSharp.Control.FSharpAsync.RunSynchronously[T] (Microsoft.FSharp.Control.FSharpAsync`1[T] computation, Microsoft.FSharp.Core.FSharpOption`1[T] timeout, Microsoft.FSharp.Core.FSharpOption`1[T] cancellationToken) [0x0006e] in <a7bd76b1e4bb5e55f477e28dcb1ac56c>:0
10-20 21:14:03.480 I/mono-stdout(15168): at ColPatApp.AppModule.update[a] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00119] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:79
10-20 21:14:03.480 I/mono-stdout(15168): at [email protected] (ColPatApp.AppModule+Msg msg, ColPatApp.AppModule+Model model) [0x00000] in C:\Users\vlonc_000\Documents\08 code\xamarin\ColPatApp\ColPatApp\ColPatApp.fs:99
10-20 21:14:03.480 I/mono-stdout(15168): at Fabulous.ProgramModule+withConsoleTrace@203-1[model,msg,arg].Invoke (msg msg, model model) [0x00082] in <431f22c722e5131029cce0ef5b1b28fe>:0
10-20 21:14:03.481 I/mono-stdout(15168): at Fabulous.ProgramRunner`3[arg,model,msg].processMsg (msg msg) [0x00000] in <431f22c722e5131029cce0ef5b1b28fe>:0
10-20 21:14:03.481 I/mono-stdout(15168): ---> (Inner Exception #0) Xamarin.Essentials.PermissionException: Permission request must be invoked on main thread.
10-20 21:14:03.481 I/mono-stdout(15168): at Xamarin.Essentials.Permissions+BasePlatformPermission.RequestAsync () [0x001fd] in D:\a\1\s\Xamarin.Essentials\Permissions\Permissions.android.cs:116 <---
10-20 21:14:03.481 I/mono-stdout(15168):
10-20 21:14:03.481 I/Choreographer(15168): Skipped 144 frames! The application may be doing too much work on its main thread.
10-20 21:14:03.512 I/OpenGLRenderer(15168): Davey! duration=2436ms; Flags=0, IntendedVsync=42641115447753, Vsync=42643515447657, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=42643522566600, AnimationStart=42643522700700, PerformTraversalsStart=42643523353900, DrawStart=42643523871900, SyncQueued=42643525037300, SyncStart=42643526420600, IssueDrawCommandsStart=42643526495500, SwapBuffers=42643549248400, FrameCompleted=42643553044100, DequeueBufferDuration=414000, QueueBufferDuration=779000,
Thread finished: <Thread Pool> #5
The thread 0x5 has exited with code 0 (0x0).
Basic Information
- IDE: Visual Studio 2019
- Platform Target Frameworks:
- Android: Pixel 2 Pie 9.0 – API 28
Yes, I will document this. You must call this from the UI thread as it is a UI operation and permission requests need the main thread. You can make these calls ahead of time before calling CaptureAsync and then call from wherever, but you will want to use the UI thread.
@jamesmontemagno perhaps this could be included on the docs, your answer is a bit difficult to find. On iOS Permissions.RequestAsync crashes without any message if called from async Task... Thanks a lot for all the nice work!