Search code examples
vb.netwindows-phone-8galleryfilepicker

Filepicker in WP8.1 app did not show any photo


I need to make instrument, that allows users to choose photo from Gallery. After choosing photo, it will be shown to the user in ImageBox.

Problem is, that when user chooses some photo in Gallery, Gallery closes, ImageBox stays empty. Code returns no error. Please help me to find error and solve this problem. Thank you.

Here is a code:

ImagePath = String.Empty

    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()


        Dim BitmapImage = New BitmapImage()
        Await BitmapImage.SetSourceAsync(filePicker)
        MyPhoto.Source = BitmapImage

Solution

  • If you are using filePicker.PickSingleFileAndContinue() you need to add code into the App_Activated

    You will notice that PickSingleFileAndContinue() is a void method will not return the picked file were as PickSingleFileAsync() will return a file.

    Code block is in C#,sorry i have only limited knowledge in vb, you can find vb sample below

    Similar SO Answer

    C#

    FileOpenPicker openPicker = new FileOpenPicker();
    openPicker.ViewMode = PickerViewMode.Thumbnail;
    openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    openPicker.FileTypeFilter.Add(".jpg");
    openPicker.FileTypeFilter.Add(".jpeg");
    openPicker.FileTypeFilter.Add(".png");
    
    StorageFile file = await openPicker.PickSingleFileAsync();
    

    As your are using PickSingleFileAndContinue() you need to implement ContinuationManager that were you get the file you picked.

    In the App.xaml.cs

    public ContinuationManager ContinuationManager { get; private set; }
    

    This will fire when the app get activated

        protected async override void OnActivated(IActivatedEventArgs e)
        {
            base.OnActivated(e);
    
            continuationManager = new ContinuationManager();
    
            Frame rootFrame = CreateRootFrame();
            await RestoreStatusAsync(e.PreviousExecutionState);
    
            if(rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MainPage));
            }
    
            var continuationEventArgs = e as IContinuationActivatedEventArgs;
            if (continuationEventArgs != null)
            {
                Frame scenarioFrame = MainPage.Current.FindName("ScenarioFrame") as Frame;
                if (scenarioFrame != null)
                {
                    // Call ContinuationManager to handle continuation activation
                    continuationManager.Continue(continuationEventArgs, scenarioFrame);
                }
            }
    
            Window.Current.Activate();
        }
    

    Usage in the page

    Assume that one page of your app contains code that calls a FileOpenPicker to pick an existing file. In this class, implement the corresponding interface from the ContinuationManager helper class. When your app uses a FileOpenPicker, the interface to implement is IFileOpenPickerContinuable.

     public sealed partial class Scenario1 : Page, IFileOpenPickerContinuable
    {
        ...
    
    //inside this you have this
    
    private void PickAFileButton_Click(object sender, RoutedEventArgs e)
        {
            ...
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");
    
            // Launch file open picker and caller app is suspended
            // and may be terminated if required
            openPicker.PickSingleFileAndContinue();
        }
    }
    
    
    switch (args.Kind)
        {
            case ActivationKind.PickFileContinuation:
                var fileOpenPickerPage = rootFrame.Content as IFileOpenPickerContinuable;
                if (fileOpenPickerPage != null)
                {
                    fileOpenPickerPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
                }
                break;
    
            ...
        }
    

    Download msdn sample here

    Msdn documentation using FilePicker