locked
C# WP8: FileOpenPicker returns absolute path, but to load an image you need a relative path?

    Question

  • Hi,

    Can anyone help me?:

    I'm developing an app that let's the user select a set of pictures from the "Pictures" folder using the FileOpenPicker class. I have no problems in displaying the pictures right afterwards, as I just get the image right from the file and set it to a Bitmap

    Windows.Storage.FileProperties.StorageItemThumbnail thumb = await args.Files[0].GetScaledImageAsThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 49);
    BitmapImage tmpbmp = new BitmapImage();
    tmpbmp.SetSource(thumb);
    ...
    

    That all works great!

    However, I want to store the selection so that the user can see the images again when he re-opens the app a second time and here I have a problem: I tried storing the path of the image (as ImageSource cannot be serialised) using

    string selimgpath = args.Files[0].Path;
    

    and then retrieve the picture from that path again using

    Windows.Storage.KnownFolders.PicturesLibrary.GetFolderAsync

    and then iterate through the folders of selimgpath to get to the file, and then load it from the PicturesLibrary this way - I have to do this, because I cannot access the folder directly, since args.Files[0].Path will return the absolutepath (WHY?).

    So yeah, absolute path is the first problem, since I get something like

    C:\Data\Users\Public\Pictures\Saved Pictures\MyPicture.jpg

    as path, I cannot simply iterate through the folders denoted by "\". I'm afraid just removing everything before "\Pictures\" will not work with other region-settings (it's probably localised, right?), and I cannot compare against

    Windows.Storage.KnownFolders.PicturesLibrary.Path

    either, since the "known" folders have no publicly accessible path...

    Any ideas?

    Thanks,

    Lewis

    Wednesday, September 3, 2014 7:27 PM

Answers

  • Wednesday, September 3, 2014 7:33 PM
    Owner
  • You don't need to serialize the AccessCache. Its purpose is to cache the access between sessions.

    If the StorageFile targets a library location that you have capability based access to then you can remember the path relative to the library, but with the issues you called out in your post. This won't work with files returned from a picker since the point of the pickers is that the user can pick from anywhere and there is no guarantee that they will come from a folder that you can access via capabilities.

    --Rob

    Wednesday, September 3, 2014 7:52 PM
    Owner

All replies

  • Wednesday, September 3, 2014 7:33 PM
    Owner
  • Thanks a lot for the quick answer, Rob - I understand the principle, but the Windows.Storage.AccessCache is not serialisable either, so that only works as long as the app is running, not between sessions. You write "Converting a StorageFile to a path to remember it for later is often workable..." - can you tell me how?

    Wednesday, September 3, 2014 7:40 PM
  • You don't need to serialize the AccessCache. Its purpose is to cache the access between sessions.

    If the StorageFile targets a library location that you have capability based access to then you can remember the path relative to the library, but with the issues you called out in your post. This won't work with files returned from a picker since the point of the pickers is that the user can pick from anywhere and there is no guarantee that they will come from a folder that you can access via capabilities.

    --Rob

    Wednesday, September 3, 2014 7:52 PM
    Owner