Xamarin表格操作相机给出空例外

时间:2017-09-12 19:02:12

标签: c# android xamarin xamarin.forms

我正在使用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连接到我的电脑进行远程调试,但它给了我同样的错误。我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

我通过简单地选择要编译的正确Android版本来解决此问题。插件文档说Android的编译版本需要设置为Android 6.0,我把它设置为7.0因为我认为这是可能的。但事实并非如此。

目标Android版本也设置为更高版本。将这两者设置为Android 6.0修复了该问题。

有关详细信息,请参阅文档here