1

Net 8.0 + MS Toolkit, MVVM, VS 2022, android emulator 11 API 30

I've a listview based on a ObservableCollection of class

public partial class ADVImage : BaseModel //<= extends ObservableObject { [ObservableProperty] private int _ADVId; [ObservableProperty] private string _description; [ObservableProperty] private ImageSource _imgData; } 

here I've a command

public partial class ADVModel : BaseModel //<= extends ObservableObject { public ADVModel() { Images = new ObservableCollection<ADVImage>(); } [ObservableProperty] private string _titolo; [ObservableProperty] private string _testo; public ObservableCollection<ADVImage> Images { get; set; } [RelayCommand] private async void AddImage() //Button in page "Add" { try { var result = await MediaPicker.Default.PickPhotoAsync(); if (result != null) { if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) || result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase)) { var newFile = Path.Combine(FileSystem.CacheDirectory, result.FileName); using (var stream = await result.OpenReadAsync()) { using (var newStream = File.OpenWrite(newFile)) { await stream.CopyToAsync(newStream); // <=== HERE } ADVImage aDV = new ADVImage(); aDV.ImgData = ImageSource.FromFile(newFile); aDV.Description = "New image"; Images.Add(aDV); } } } } catch (Exception ex) { // The user canceled or something went wrong } } } 

When I reach the point where the stream has to be copied I receive this error:

"System.ObjectDisposedException: 'Cannot access a closed Stream.'"

But why? I've never accessed

In android log I cannot find any error related...

Tryed to download MAUI official samples fron Github and copy-paste code from "PlatformIntegrationDemo".. same issue/same error ...

No solutions working.. chaged Mediapicker with FilePicker (same issue)

11
  • why not just use File.Copy? Commented May 3, 2024 at 18:05
  • Have you tried debugging to check where does the object get disposed exactly? Also check if there are any inner exceptions Commented May 4, 2024 at 0:34
  • 1
    @FreakyAli file are not disposed (by me, mabe internally in ... come place), exception is raised where I wrote "HERE" but before I never read or something on result (only check if it's null and its name). This makes me crazy. But I've seen a lot of simililar troubles (via Google) Commented May 5, 2024 at 8:57
  • 1
    EDIT: Simplified code var result = await MediaPicker.Default.PickPhotoAsync(new MediaPickerOptions(); if (result != null) { ADVImage aDV = new ADVImage(); aDV.ImageData = File.ReadAllBytes(result.FullPath); aDV.Description = "Newi Image"; } Byte array lenght is correct (>0) but AFTER code IS COMPLETED same error on stream (???) Commented May 5, 2024 at 9:05
  • 2
    Tryed to download MAUI official samples fron Github and copy-paste code from "PlatformIntegrationDemo".. same issue/same error I did a test by using the official sample PlatformIntegrationDemo you mentioned above on my android emulator 11 (API 30), but I couldn't reproduce this problem. How can we reproduce this problem? Could you please post the steps of reproducing this problem? Commented May 18, 2024 at 5:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.