简体   繁体   English

通过fileopenpicker获取所选图像的路径Windows Phone 8.1 C#

[英]get path of selected image by fileopenpicker windows phone 8.1 C#

I got an image by using FileOpenPicker. 我通过使用FileOpenPicker获得了图像。 I want to save the path of the image in database, so that by using that path I can get that image any other screen,, 我想将图像的路径保存在数据库中,以便使用该路径可以在任何其他屏幕上获取该图像,

Now I simply send my image path to second page. 现在,我只需将图像路径发送到第二页。 but with the same Path I'm not able to get image. 但是使用相同的路径我无法获取图像。

Here is my code. 这是我的代码。

  private async void button_Click(object sender, RoutedEventArgs e)
    {

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


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


            **//ImagePath is global string. I get the image path here** 


            ImagePath = storageFile.Path;

            var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
            await bitmapImage.SetSourceAsync(stream);

            var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

           //image is my image control in Xaml. 
            image.Source = bitmapImage;
        }

    }

    private void image_click(object sender, TappedRoutedEventArgs e)
    {
        this.Frame.Navigate(typeof(detail), ImagePath);
    }

By tapping on Image I move to detail screen. 通过点击图像,我进入细节屏幕。 where I have another image control where I wan to show the same image ,,,,getting through path. 在这里我有另一个图像控件,在这里我要显示通过路径的相同图像。

My Detail screen code is: 我的详细信息屏幕代码是:

    protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        var imagePath = e.Parameter as string;
         image2.Source = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
    }

The above code didn't work. 上面的代码不起作用。

Then I tried another way,, 然后我尝试了另一种方式,

        var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri(imagePath));

        Stream stream = await file.OpenStreamForReadAsync();
        using (var memStream = new MemoryStream())
        {
            await stream.CopyToAsync(memStream);
            memStream.Position = 0;

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());

            // create a new stream and encoder for the new image
            InMemoryRandomAccessStream mrAccessStream = new InMemoryRandomAccessStream();
            BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(mrAccessStream, decoder);

            // convert the bitmap to a 400px by 600px bitmap
            encoder.BitmapTransform.ScaledHeight = 400;
            encoder.BitmapTransform.ScaledWidth = 600;

            try
            {
                await encoder.FlushAsync();
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }

            // render the stream to the screen
            WB = new WriteableBitmap(500, 500);
            WB.SetSource(mrAccessStream);

            image2.Source = WB;

This also didn't work. 这也没有用。 I think the problem is in path. 我认为问题出在道路上。 I got path awkward path like 我的路径很尴尬

      "C:\\Data\\Users\\Public\\Pictures\\Saved Pictures\\Image-130872081698409356.jpeg".

Please help me to get rid of this problem. 请帮助我摆脱这个问题。

This is by design and it is a security issue. 这是设计使然,是安全问题。 Imagine what would happen if just knowing the path to a file you could access that file. 想象一下,如果仅知道文件路径就可以访问该文件,将会发生什么。 The security model of store apps would be broken. 商店应用程序的安全模型将被破坏。

Note that is the file picker that provides you with a file that you have access to. 请注意,该文件选择器为您提供了您有权访问的文件。 You can start from a file picker to get access to a file. 您可以从文件选择器开始以访问文件。 You cannot start from a path to get access to a file. 您不能从路径开始获取对文件的访问。 A file picker guarantees that the user will be involved and hence aware of what is going on. 文件选择器保证用户会参与其中,从而了解发生了什么。 If you were allowed to start from a path and programmatically access a file that would violate the security rules for app stores. 如果允许您从路径开始并以编程方式访问会违反应用商店安全性规则的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM