2017-05-09 17 views
0

James Montemagnoのメディアプラグインを使用しています。私はプラグインをインストールし、必要なすべての権限を追加しました。私は私のアプリを実行したときしかし、私は次のコードの最初の行にnull参照の例外を取得:null参照例外 - Takephotoasync xamarin

image.Source = ImageSource.FromStream(() =>{var stream = file.GetStream();file.Dispose();return stream;});

私は運とほぼ2日間フルのためのソリューションを探してみました! ご迷惑をおかけいたします。 ありがとうございます!

+0

あなたがブレークポイントを入れましたか? 'image'がヌルか' file'かどうかを判断できますか? –

+0

もっとコードを表示できますか?一行から何が起こっているのかを知るのは難しいです。 – Adam

答えて

1

私はあなたがプラグインを使用する方法のサンプルを見つけることができますdownload the source

HEREを示唆しています。

var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions 
    { 
     PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium, 
     Directory = "Sample", 
     Name = "test.jpg" 
    }); 

    if (file == null) 
     return; 

    DisplayAlert("File Location", file.Path, "OK"); 

    image.Source = ImageSource.FromStream(() => 
    { 
     var stream = file.GetStream(); 
     file.Dispose(); 
     return stream; 
    }); 
    }; 
0

ImageSource.FromStreamは常に作成されたストリームを解放するので、私は同じ問題を持っていた...ここで写真を撮ると、バインドさListViewコントロール内の画像を表示する私のコードです。要するに、私はプラグインから元のストリームを取得し、それをバイト[]に変換し、それを専用オブジェクトに格納します。ピクチャを画像ソースに表示する必要があるときは、byte []から新しいストリームを作成し、Xamarin.FormsのImageSource.FromStreamメソッドで使用します。

public class PictureModel 
    { 
     public byte[] File { get; set; } 
     public ImageSource ImageSource => ImageSource.FromStream(() => new MemoryStream(File)); 
    } 

    public class PicturesViewModel : ViewModelBase 
    { 
     public ObservableCollection<PictureModel> Pictures { get; set; } = new ObservableCollection<PictureModel>(); 

     public ICommand TakePictureCommand => new Command(async() => 
     { 
      var stream = await TakePictureAsync(); 
      var picture = new PictureModel { File = await ImageConverter.ReadFully(stream) }; // custom method to convert stream into byte[] 
      Pictures.Add(picture); 
     }); 

     protected async Task<Stream> TakePictureAsync() 
     { 
      await CrossMedia.Current.Initialize(); 

      if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported) 
      { 
       return null; 
      } 

      var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions 
      { 
       SaveToAlbum = false, 
       PhotoSize = PhotoSize.Medium 
      }); 

      if (file == null) 
       return null; 

      var stream = file.GetStream(); 
      file.Dispose(); 
      return stream; 
     } 
    } 

たぶん私は、私はまだテストしていない TakePictureCommandでローカルストリームを配置する必要がありますが。

とXAML:

<ListView x:Name="listView" ItemsSource="{Binding Pictures}" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" RowHeight="200"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
       <StackLayout Orientation="Horizontal"> 
        <Image Source="{Binding ImageSource}" WidthRequest="400" HeightRequest="200" /> 
       </StackLayout> 
      </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 
+0

ここはXamarinの問題です。https://forums.xamarin.com/discussion/18994/imagesource-fromstream-dispose – hugoterelle