Xamarin Forms - 使用 MediaGallery 插件将多个图像发送到文件流

发布于 2025-01-12 05:16:23 字数 2300 浏览 2 评论 0原文

我正在开发 Xamarin Forms 应用程序,并且添加了一些代码来从手机图库中选择多个图像,然后需要将所有选定的图像发送到文件 steam。

从图库中选择多个图像工作正常,但仅发送最后选择的图像。

下面是我的代码

private async Task OnGetExistingPhotoAsync() {
  try {
    // Selecting multiple images with the MediaGallery plugin

    var results = await MediaGallery.PickAsync(3, MediaFileType.Image);
    var parameter = new NavigationParameters();

    if (results?.Files == null) {
      return;
    }

    foreach(var photo in results.Files) {
      if (photo == null) {
        PhotoPath = null;
        return;
      }

      string newFile = Path.Combine(FileSystem.CacheDirectory, photo.NameWithoutExtension);
      using(var stream = await photo.OpenReadAsync())
      using(var newStream = File.OpenWrite(newFile))
      await stream.CopyToAsync(newStream);
      PhotoPath = newFile;

      // This here is only sending the PATH of the last selected image from the gallery
      parameter.Add("PhotoPath", PhotoPath);


      // In my attempt here, I tried to add each image PATH to a list Array
      // But this is not working!
      /*
      List<string> imageFiles = new List<string>();
      imageFiles.Add(Path.Combine(FileSystem.CacheDirectory, photo.NameWithoutExtension));

      String[] newFile = imageFiles.ToArray();

      using (var stream = await photo.OpenReadAsync())
      using (var newStream = File.OpenWrite(newFile)) // This here says that it cannot convert ( string[] to string )
          await stream.CopyToAsync(newStream);

      parameter.Add("PhotoPath", PhotoPath);
      */

    }

    //OUTPUT :  The PATH is : /data/user/0/se.company.trret.cyn/cache/IMG_20220307_005342
    Console.WriteLine("The PATH is : " + PhotoPath);

    await _navigationService.GoBackAsync(parameter);
  } catch (FeatureNotSupportedException fnsEx) {
    await App.Current.MainPage.DisplayAlert("Ett fel inträffade", "Den här funktionen stöds inte av din enhet.", "Ok");
  } catch (PermissionException pEx) {
    await App.Current.MainPage.DisplayAlert("Ett fel inträffade", "Cykelstaden saknar rättigheter för att läsa dina filer.", "Ok");
  } catch (Exception e) {
    await App.Current.MainPage.DisplayAlert("Ett fel inträffade", "Ett oväntat fel inträffade. Var god försök igen.", "Ok");
    Crashes.TrackError(e);
  }
} 

I'm working on an Xamarin Forms application and I have added some code to select multiple images from the phone gallery, I then need to send all selected images to file steam.

Selecting multiple images from the gallery is working fine, but only the last selected image is being sent.

Below is my code

private async Task OnGetExistingPhotoAsync() {
  try {
    // Selecting multiple images with the MediaGallery plugin

    var results = await MediaGallery.PickAsync(3, MediaFileType.Image);
    var parameter = new NavigationParameters();

    if (results?.Files == null) {
      return;
    }

    foreach(var photo in results.Files) {
      if (photo == null) {
        PhotoPath = null;
        return;
      }

      string newFile = Path.Combine(FileSystem.CacheDirectory, photo.NameWithoutExtension);
      using(var stream = await photo.OpenReadAsync())
      using(var newStream = File.OpenWrite(newFile))
      await stream.CopyToAsync(newStream);
      PhotoPath = newFile;

      // This here is only sending the PATH of the last selected image from the gallery
      parameter.Add("PhotoPath", PhotoPath);


      // In my attempt here, I tried to add each image PATH to a list Array
      // But this is not working!
      /*
      List<string> imageFiles = new List<string>();
      imageFiles.Add(Path.Combine(FileSystem.CacheDirectory, photo.NameWithoutExtension));

      String[] newFile = imageFiles.ToArray();

      using (var stream = await photo.OpenReadAsync())
      using (var newStream = File.OpenWrite(newFile)) // This here says that it cannot convert ( string[] to string )
          await stream.CopyToAsync(newStream);

      parameter.Add("PhotoPath", PhotoPath);
      */

    }

    //OUTPUT :  The PATH is : /data/user/0/se.company.trret.cyn/cache/IMG_20220307_005342
    Console.WriteLine("The PATH is : " + PhotoPath);

    await _navigationService.GoBackAsync(parameter);
  } catch (FeatureNotSupportedException fnsEx) {
    await App.Current.MainPage.DisplayAlert("Ett fel inträffade", "Den här funktionen stöds inte av din enhet.", "Ok");
  } catch (PermissionException pEx) {
    await App.Current.MainPage.DisplayAlert("Ett fel inträffade", "Cykelstaden saknar rättigheter för att läsa dina filer.", "Ok");
  } catch (Exception e) {
    await App.Current.MainPage.DisplayAlert("Ett fel inträffade", "Ett oväntat fel inträffade. Var god försök igen.", "Ok");
    Crashes.TrackError(e);
  }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文