Windows Phone 8.1中的文件选择器

时间:2014-07-13 05:22:19

标签: c# windows-phone-8.1

我想从Windows Phone 8.1中的图片相册中选择一张图片。为此我使用了这段代码,但它给出了错误

private async void gallery_Tapped(object sender, TappedRoutedEventArgs e)
        {
            FileOpenPicker opener = new FileOpenPicker();
            opener.ViewMode = PickerViewMode.Thumbnail;
            opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            opener.FileTypeFilter.Add(".jpg");
            opener.FileTypeFilter.Add(".jpeg");
            opener.FileTypeFilter.Add(".png");

            StorageFile file = await opener.PickSingleFileAsync();
            if (file != null)
            {
                // We've now got the file. Do something with it.
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                await bitmapImage.SetSourceAsync(stream);

                var decoder = await               Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
                MyImage.Source=bitmapImage;
            }
            else
            {
                //OutputTextBlock.Text = "The operation may have been cancelled.";
            }
        }

错误

enter image description here

4 个答案:

答案 0 :(得分:15)

我认为即使在您需要的页面中也可以处理OnActivated事件。像这样的东西

CoreApplicationView view = CoreApplication.GetCurrentView();

ImagePath=string.Empty;
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;

// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");

filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated; 

private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
    FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;

    if (args != null)
    {
        if (args.Files.Count == 0) return;

        view.Activated -= viewActivated;
        storageFileWP = args.Files[0];

    }
}

当您从选择器中选择文件时,将调用上述方法。我相信它可以帮到你。

答案 1 :(得分:12)

使用Windows Phone 8.1中的FileOpenPicker从图片库中选择图片。

步骤1:在Windows Phone 8.1应用程序中添加图片库功能。

Picture Library Capability

第2步:添加文件打开选择器作为声明。

File Open Picker declaration

步骤3:向MainPage.xaml添加按钮和图像。

<Grid>
<Image Name="img"/>
<Button Content="click me" Click="Button_Click"/>
</Grid>

步骤4:添加全局变量视图。

CoreApplicationView view;

步骤4.1在页面构造函数中初始化。

view = CoreApplication.GetCurrentView();

步骤5:添加代码以在Button Click事件上调用文件打开选择器。

private void Button_Click(object sender, RoutedEventArgs e)
{
    FileOpenPicker filePicker = new FileOpenPicker();
    filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    filePicker.ViewMode = PickerViewMode.Thumbnail;

    // Filter to include a sample subset of file types
    filePicker.FileTypeFilter.Clear();
    filePicker.FileTypeFilter.Add(".bmp");
    filePicker.FileTypeFilter.Add(".png");
    filePicker.FileTypeFilter.Add(".jpeg");
    filePicker.FileTypeFilter.Add(".jpg");

    filePicker.PickSingleFileAndContinue();
    view.Activated += viewActivated; 
}

步骤6:在View激活的事件上将图像设置为MainPage。

private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
  FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;

  if (args != null)
  {
      if (args.Files.Count == 0) return;

      view.Activated -= viewActivated;
      StorageFile storageFile = args.Files[0];
      var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
      var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
      await bitmapImage.SetSourceAsync(stream);

      var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
      img.Source=bitmapImage;
  }
}

它还允许您拍照并使用它。

参考: Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery

答案 2 :(得分:3)

在wp 8.1 xaml中使用RoutedEventArgs代替TappedRoutedEventArgs按钮 不要使用异步关键字

private void OpenImageFile(object sender, RoutedEventArgs e)
{            
            FileOpenPicker filePicker = new FileOpenPicker();
            filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            filePicker.ViewMode = PickerViewMode.Thumbnail;

            // Filter to include a sample subset of file types
            filePicker.FileTypeFilter.Clear();
            filePicker.FileTypeFilter.Add(".bmp");
            filePicker.FileTypeFilter.Add(".png");
            filePicker.FileTypeFilter.Add(".jpeg");
            filePicker.FileTypeFilter.Add(".jpg");

            filePicker.PickSingleFileAndContinue();
            view.Activated += viewActivated;
}

private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
            FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;

            if (args != null)
            {
                if (args.Files.Count == 0) return;

                view.Activated -= viewActivated;
                StorageFile SelectedImageFile = args.Files[0];

            }
}

  • 并使用CoreApplicationView视图;在每个方法的类外面的任何地方作为全局
  • 不要忘记使用view = CoreApplication.GetCurrentView();在InitializeComponent()之后的相关页面类的构造函数内部;方法

我认为这会有所帮助:)谢谢

答案 3 :(得分:0)

var fill = await StorageFile.GetFileFromPathAsync(selectItem.FolderPath);                         BitmapImage bit = new BitmapImage();

                    if (fill != null)
                    {
                        // We've now got the file. Do something with it.
                        var stream = await fill.OpenAsync(Windows.Storage.FileAccessMode.Read);
                        //var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                        //await bitmapImage.SetSourceAsync(stream);
                        bit.SetSource(stream);
                        imgTeste.Source = bit;
                        pvMestre.SelectedIndex = 1;
                    }
                    else
                    {
                        //OutputTextBlock.Text = "The operation may have been cancelled.";
                    }