Xamarin Forms 操作相机时出现空异常

3
我正在使用Xamarin Forms和Visual Studio开发移动Android应用程序。
我正在使用CrossMedia插件,以便在我的移动应用程序中拍摄或选择照片。起初,我遇到了初始化问题,原因是我所针对的Android SDK错误。在我更新SDK并更新所有软件包后,我能够使“选择照片”选项工作,但使用相机仍然无法正常工作,我无法弄清楚是什么原因导致这种情况。
我有以下方法;
private async void TakeAPhoto(object sender, EventArgs e)
{
    try
    {
        await CrossMedia.Current.Initialize();
    }
    catch (Exception exception)
    {
        await DisplayAlert("ERROR", "Error initializing camera!", "Ok");
    }

    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 (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
    {
        await DisplayAlert("No camera", "No camera available", "Ok");
        return;
    }

    if (cameraStatus == PermissionStatus.Granted && storageStatus == PermissionStatus.Granted)
    {
        MediaFile file;
        try
        {
            //Exception occurs in this code.
            file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                //Specify Store to Album OR Directory, not both
                Directory = "App_Images",
                Name = "Test.jpg"

            });
        }
        catch (Exception exception)
        {
            //I've got a break point here which is being hit, but the exception is (null)
            throw;
        }

        if (file == null)
            return;

        //TODO: Store image to azure.
    }
    else
    {
        await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK");
        //On iOS you may want to send your user to the settings screen.
        //CrossPermissions.Current.OpenAppSettings();
    }
}

然而,当我运行代码时,出现了一个空异常,只显示“(null)”;

enter image description here

Visual Studio的调试窗口给了我很多信息,但我在这里看到的唯一真正的异常是“InvocationException”。
InspectorDebugSession(0): HandleTargetEvent: TargetHitBreakpoint
InspectorDebugSession(0): StateChange: EntryPointBreakpointRegistered -> EntryPointBreakpointHit
InspectorDebugSession(0): AgentBridge.InjectAssembly: /mnt/shell/emulated/0/Android/data/MyFirstAppPackage.MyFirstAppPackage/files/.__override__/inspector-temp/Xamarin.Interactive.dll
InspectorDebugSession(0): AgentBridge.InjectAssembly: Mono.Debugger.Soft.InvocationException: Exception of type 'Mono.Debugger.Soft.InvocationException' was thrown.
   at Mono.Debugger.Soft.InvocationsAPI.EndInvokeMethodInternalWithResultImpl(IAsyncResult asyncResult)
   at Xamarin.Interactive.IdeSupport.AgentBridge.InjectAssembly(String agentAssemblyPath) in C:\d\lanes\4699\fec6f88f\source\xamarinvs\External\inspector-ide-integration\Xamarin.Interactive.IdeSupport\AgentBridge.cs:line 55
   at Xamarin.Interactive.IdeSupport.InspectorDebuggerSession.<HandleTargetEvent>b__26_0(Object <p0>) in C:\d\lanes\4699\fec6f88f\source\xamarinvs\External\inspector-ide-integration\Xamarin.Interactive.IdeSupport\InspectorDebuggerSession.cs:line 242
InspectorDebugSession(0): StateChange: EntryPointBreakpointHit -> Error
InspectorDebugSession(0): Disposed

我已经忙了很长时间,试图弄清楚这个问题,但目前完全卡住了。我还尝试通过将三星Galaxy S4 Mini连接到计算机进行远程调试,但它给我同样的错误。我在这里做错了什么?


也许是一个调试工件。我已经看到了mono、模拟器/设备和Visual Studio的组合比这更奇怪的事情。尝试使用Debug.Write(exception.ToString());而不是throw,看看是否输出任何有用的信息。 - Cee McSharpface
我在Android模拟器上运行了你的函数,没有问题,它正常工作。这是一个已知的问题,即在一个函数中有相同名称的变量。你有两个“exception”变量。将其中一个改为“ex1”,你就能看到一个异常了。 - Yuri S
正如Yuri所说,有两种异常:System.ExceptionJava.Lang.Exception。我不知道你正在使用哪种类型的Exception,但似乎你捕获的异常并不属于你正在使用的Exception - York Shen
1个回答

0

我通过简单选择适当的Android版本进行编译解决了这个问题。插件文档说,android的编译版本需要设置为Android 6.0,我将其设置为7.0,因为我以为这是可能的。但事实并非如此。

此外,目标Android版本也被设置为更高的版本。将这两个都设置为Android 6.0可以解决问题。

有关详细信息,请参见此处的文档。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接