如何使用filepicker获取所有照片

时间:2014-12-31 07:30:58

标签: c# windows-runtime windows-phone-8.1

我制作了一个简单的窗口手机8.1应用程序我希望在应用程序中显示aLl照片,然后用户选择PickMultipleFilesAndContinue .....但我不知道该怎么做。我做了这个代码openfiler选择器带我到手机库.....有没有其他方法来获取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.";
            }

    }

通过这个imting只选择了照片。我希望所有人都能显示,然后用户从中选择......

1 个答案:

答案 0 :(得分:1)

您可以通过编程方式访问照片,然后将它们添加到ImageCollection中。我已经调整了照片的大小,因为否则应用程序会在我的手机上崩溃。

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);
        }
    }
}