如何在没有文件选择器的情况下获取所有照片
本文关键字:情况下 获取 照片 文件 选择器 | 更新日期: 2023-09-27 18:35:41
我正在制作一个简单的窗口手机 8.1 应用程序,我想让 aLl 照片显示在应用程序中,然后用户选择选择多个文件并继续.....但我不知道该怎么做.我做了这个代码 openfiler picker 带我去电话库.....还有其他方法可以在Windows Phone 8.1中获取照片吗?
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
openPicker.PickMultipleFilesAndContinue();
view.Activated += view_Activated;
}
private async void view_Activated(CoreApplicationView sender, Windows.ApplicationModel.Activation.IActivatedEventArgs args1)
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
bitmapImages = new ObservableCollection<BitmapImage>();
IReadOnlyList<StorageFile> files = args.Files;
if (files.Count > 0)
{
StringBuilder output = new StringBuilder("Picked files:'n");
// Application now has read/write access to the picked file(s)
foreach (StorageFile file in files)
{
output.Append(file.Name + "'n");
using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
try
{
BitmapImage bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
bitmapImage.DecodePixelHeight = 200;
bitmapImage.SetSource(stream);
bitmapImages.Add(bitmapImage);
}
catch (ArgumentException Ex)
{
Debug.WriteLine("Exception ", Ex.Message);
}
}
}
ImageCollection.ItemsSource = bitmapImages;
OutputTextBlock.Text = output.ToString();
}
else
{
OutputTextBlock.Text = "Operation cancelled.";
}
}
通过这个我只得到选定的照片. 我希望全部显示,然后用户从中选择.....
您可以通过编程方式访问照片,然后将它们添加到图像收藏中。我已经调整了照片的大小,否则应用程序会在我的手机上崩溃。
StorageFolder pictureFolder = KnownFolders.PicturesLibrary; //or another folder
IReadOnlyList<IStorageItem> nameList = await pictureFolder.GetItemsAsync();
var bitmapImages = new ObservableCollection<BitmapImage>();
foreach (var item in nameList)
{
if (item is StorageFile)
{
if (item.Name.Substring(item.Name.Length - 4, 3).ToLower() == "jpeg" || item.Name.Substring(item.Name.Length - 3, 3).ToLower() == "jpg" || item.Name.Substring(item.Name.Length - 3, 3).ToLower() == "png")
{
Image image = new Image();
StorageFile file = await pictureFolder.GetFileAsync(item.Name);
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(fileStream);
if (bitmapImage.DecodePixelHeight >= bitmapImage.DecodePixelWidth)
{
bitmapImage.DecodePixelWidth = bitmapImage.DecodePixelHeight / 100;
bitmapImage.DecodePixelHeight = 100;
}
else
{
bitmapImage.DecodePixelHeight = bitmapImage.DecodePixelWidth / 100;
bitmapImage.DecodePixelWidth = 100;
}
bitmapImages.Add(bitmapImage);
}
}
}