【问题标题】:Xamarin forms operating camera gives empty exceptionXamarin 表单操作相机给出空异常
【发布时间】:2017-09-18 02:07:12
【问题描述】:

我正在使用 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)';

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

【问题讨论】:

  • 可能是调试的产物。我已经看到单声道、模拟器/设备和视觉工作室的组合做了比这更奇怪的事情。尝试使用Debug.Write(exception.ToString()); 而不是throw,看看它是否会输出任何有用的信息。
  • 我在 Android 模拟器上运行你的函数没有问题,它工作正常。在一个函数中具有相同名称的变量是已知的问题。您有 2 个“异常”变量。将一个更改为“ex1”,您将能够看到一个异常。
  • 正如 Yuri 所说,有两种异常:System.Exception 和 Java.Lang.Exception。我不知道你使用的是什么类型的Exception,但是你捕获的异常似乎不属于你使用的Exception。

标签: c# android xamarin xamarin.forms


【解决方案1】:

我只需选择合适的 Android 版本进行编译即可解决此问题。插件文档说需要将android的编译版本设置为Android 6.0,我将其设置为7.0,因为我认为这是可能的。但事实并非如此。

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

有关更多信息,请参阅文档here。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-29
    • 2019-06-20
    • 1970-01-01
    • 2017-05-26
    • 2011-11-28
    • 2019-10-02
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多