Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

c# - File Picker in Windows Phone 8.1

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...