1

I got an image by using 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.

Arsal
  • 343
  • 2
  • 8
  • 21
  • right. Windows Phone doesn't have such a file structure. you cannot load a file of any kind like that. the path is incorrect - this is the problem. did you see what the exception was? what was the exception? – Ahmed ilyas Sep 20 '15 at 12:45
  • for both ways there is no exception. I just did not get the image at image control. – Arsal Sep 20 '15 at 12:47
  • Would you please let me know that how to get exact path ,,so I could use that any where and get image. – Arsal Sep 20 '15 at 12:50
  • Did you try to use `StorageApplicationPermissions.MostRecentlyUsedList` or `StorageApplicationPermissions.FutureAccessList`? You'll identify a file by a token instead of the full path though, but that's how "metro" application keeps files accessible. – DK. Sep 20 '15 at 13:00
  • No I did not use this. would you please provide me a sample for this? – Arsal Sep 20 '15 at 13:05
  • Sure. MSDN has a nice article on that: [How to track recently used files and folders](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh972344.aspx). You might also want to review [File access and permissions](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh967755.aspx). – DK. Sep 20 '15 at 13:23

1 Answers1

0

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.

Ladi
  • 1,274
  • 9
  • 17
  • yup. but what would be the process to get that image ? The only way is that i can save image path ...and i will use that path to get my image at another screen. – Arsal Sep 20 '15 at 12:55
  • Load the content of that image on the first page where you get the access to the file. Store that content in memory and pass that data directly or indirectly to the next page. Then use that data on the next page. Another option is that on the first page you copy the file to the local storage that you have access to. On the second page you can access the file you copied on the local storage. – Ladi Sep 20 '15 at 13:01
  • yeah. You are right. I have to copy the image file to local storage. would you please send me any example to store image in local storage? – Arsal Sep 20 '15 at 13:14
  • I got your point , Would you please provide me the right solution for this problem – Arsal Sep 20 '15 at 13:23