Xamarin Forms - 使用 MediaGallery 插件将多个图像发送到文件流
我正在开发 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论