8

I want to pick an image from my pictures album in windows phone 8.1 . For this I used this code but its gives error

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.";
            }
        }

Error

enter image description here

Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89
  • 1
    Shouldn't you use [PickSingleFileAndContinue](http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.pickers.fileopenpicker.picksinglefileandcontinue.aspx?)? – Ulugbek Umirov Jul 13 '14 at 05:37
  • 1
    Like Ulugbek had said - you are targeting Windows Phone and you cannot use those methods (*PickSingleFileAsync()*), hence your app might be terminated while picking the file. You have to use the above method - more reference and a good example you will find [here at MSDN](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/dn614994.aspx). – Romasz Jul 13 '14 at 05:49
  • Yes I correct that but how to work with that selected file this that returns void? – Ghazanfar Khan Jul 13 '14 at 09:50
  • @user3814490 Romasz already gave you the link about ContinuationManager. – Ulugbek Umirov Jul 13 '14 at 10:42
  • I am struggling with code to write App.xaml.cs OnActivated Event – Ghazanfar Khan Jul 13 '14 at 10:45

4 Answers4

15

I think you can handle the OnActivated event even in the page where you required. Something like this

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];

    }
}

When you select the files from the picker the above method will be called. I believe it helps you.

BlaShadow
  • 11,075
  • 7
  • 39
  • 60
Sunil Kumar S C
  • 296
  • 2
  • 12
12

Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery.

Step 1: Add Picture Library Capability in your Windows Phone 8.1 app.

Picture Library Capability

Step 2: Add File Open Picker as a declaration.

File Open Picker declaration

Step 3: Add a button and image to MainPage.xaml.

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

Step 4: Add global variable view.

CoreApplicationView view;

Step 4.1 Initialize in page constructor.

view = CoreApplication.GetCurrentView();

Step 5: Add the code to call the File Open Picker on Button Click event.

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

Step 6: On View activated event set the image to the 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;
  }
}

It also allows you to take a photo and use it.

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

Roberto Orozco
  • 579
  • 7
  • 11
  • You never used ImagePath variable inside your code. What is it for? – Ali Demirci Mar 15 '16 at 11:07
  • You are right! I never used it, I don't even know why it's there, I am looking my current implementation and ImagePath is not there. I am editing my answer right now. Thanks! – Roberto Orozco Mar 17 '16 at 22:47
  • should the event handler not be removed before the count is checked, in case the user aborted the file picker with no selection? – Cee McSharpface Nov 04 '16 at 16:30
3

Use RoutedEventArgs instead of TappedRoutedEventArgs for button click in wp 8.1 xaml Don't use async key word

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];

            }
}
  • And use CoreApplicationView view; Any where in the class out side of every method as global
  • Don't forget to use view = CoreApplication.GetCurrentView(); inside the constructor of the relevant page class after InitializeComponent(); method

I think this will help :) Thanks

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.";
                    }