2017-02-13 46 views
0

我正在开发Xamarin Android项目,我想用MvvmCross实现拍照。在Xamarin Android中使用MvvmCross拍照

这里是我的代码:

public class PhotoService:IPhotoService 
{ 
    private const int MaxPixelDimension = 1280; 
    private const int DefaultJpegQuality = 90; 

    private Stream imageStream; 

    public Stream ImageStream 
    { 
     get { return imageStream; } 
     set { imageStream = value; } 
    } 

    public void GetPhoto() 
    { 
     var task = Mvx.Resolve<IMvxPictureChooserTask>(); 

     task.TakePicture(
     MaxPixelDimension, 
     DefaultJpegQuality, 
     SavePicture, null); 
    } 

    private void SavePicture(Stream stream) 
    { 
     ImageStream = stream; 
    } 

} 

但在:

task.TakePicture(
     MaxPixelDimension, 
     DefaultJpegQuality, 
     SavePicture, 
     null); 

我有错误:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

UPDATE

呼叫堆栈我有:

0x0 in Android.Content.Intent..ctor at /Users/builder/data/lanes/3511/501e63ce/source/monodroid/src/Mono.Android/platforms/android-24/src/generated/Android.Content.Intent.cs:1275,6 C# 0x12 in MvvmCross.Plugins.PictureChooser.Droid.MvxPictureChooserTask.TakePicture C#
0x3A in App.Services.PhotoService.PhotoService.GetPhoto at C:\app\App.Services\PhotoService\PhotoService.cs:38,4 C#
0x7 in App.ViewModels.ViewModels.MainViewModel.TakePhoto at C:\app\App.ViewModels\ViewModels\MainViewModel.cs:49,4 C#

+0

您是否有更多信息?如堆栈跟踪。可能是内部异常? – Cheesebaron

+0

@Cheesebaron问题已更新 – Quiet

+0

这看起来不像整个堆栈跟踪。 – Cheesebaron

回答

1

替代解决方案,您可以使用Media插件中的NuGet

https://www.nuget.org/packages/Xam.Plugin.Media/

你可以使用相关的服务来调用Android项目的takePictureAsync方法可用的。使用此库,您可以指定文件名和文件夹路径来存储图像。该库也可以使用takeVideoAsync方法拍摄视频。

+0

是的,它工作得很好。谢谢! – Quiet

0

我相信你需要的MVVMCross.Pugin.PictureChooser包添加到您的核心平台的具体项目。

+0

我只有android项目。我将此插件添加到Android(核心)和我的Android类库中,PhotoService – Quiet