我正在使用Xamarin Forms和Visual Studio开发移动Android应用程序。
我正在使用CrossMedia插件,以便在我的移动应用程序中拍摄或选择照片。起初,我遇到了初始化问题,原因是我所针对的Android SDK错误。在我更新SDK并更新所有软件包后,我能够使“选择照片”选项工作,但使用相机仍然无法正常工作,我无法弄清楚是什么原因导致这种情况。
我有以下方法;
我正在使用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
,看看是否输出任何有用的信息。 - Cee McSharpfaceSystem.Exception
和Java.Lang.Exception
。我不知道你正在使用哪种类型的Exception
,但似乎你捕获的异常并不属于你正在使用的Exception
。 - York Shen