c# - Xamarin 表格操作相机给出空异常

标签 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

我已经忙了很长一段时间来尝试解决这个问题,但目前我完全被困在了这个问题上。我还尝试通过将 Samsung Galaxy S4 Mini 连接到我的计算机进行远程调试,但它给了我同样的错误。我在这里做错了什么?

最佳答案

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

目标android版本也设置为更高版本。将这些都设置为 Android 6.0 解决了这个问题。

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

关于c# - Xamarin 表格操作相机给出空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46183657/

相关文章:

android - 从 kml 轨迹 GPS 坐标计算速度的算法

c# - Xamarin 内存分析器对象增长理解

c# - 构建一个 c# 桌面应用程序,从头开始,请提示

c# - 如何在 session 中保存 SQL 查询以备后用

android - 使用 React Native 更改键盘后面的背景颜色?

xamarin - 如果 Xamarin Forms DependencyService 有多个实现怎么办?

Xamarin Google API 客户端不存在?

c# - 模糊 C# 中的不安全代码行为

c# - Type.GetType() 带空

android - 从文件中读取对象会抛出 IllegalAccessException