none
[UWP] Why can't we show image direct even when we have permission of that image? RRS feed

  • Question

  • Hi,

    I have a fileOpenPicker/OR(via open-with) in my UWP application -> picked an image using the same. There is an ImageView in the xaml file. 

    <Image x:Name="MyImage" Width="400" Height="400" HorizontalAlignment="Center" VerticalAlignment="Center" />

    I need to show that image in the ImageView using this:

     BitmapImage img = new BitmapImage();
     Uri uri = new Uri(selectedFile.Path);
    img.UriSource = uri;
    MyImage.Source = img;

    But this doesn't work. it doesn't show the image on the screen.

    I need to first copy the picked file to Local folder using CopyAsync() and then give the path of the copied file to BitmapImage to show the image. Then it shows the image on screen.

    Am I missing something? Any help will be appreciated. 

    Thanks.


    Tuesday, May 29, 2018 10:28 AM

Answers

  • Use BitmapImage.SetSourceAsync to load the BitmapImage from the streamed data from the StorageFile. There is example code for this in the BitmapImage documentation 

    The file picker and file activation systems provide a StorageFile with brokered permissions. When you export it to a Path (which may or may not exist, but that's another problem you'll run into here) you bypass the brokered permissions and the load fails since you don't have direct permission to the file itself. I blogged about this several years ago at Skip the path: stick to the StorageFile

    Tuesday, May 29, 2018 11:38 PM

All replies

  • Use BitmapImage.SetSourceAsync to load the BitmapImage from the streamed data from the StorageFile. There is example code for this in the BitmapImage documentation 

    The file picker and file activation systems provide a StorageFile with brokered permissions. When you export it to a Path (which may or may not exist, but that's another problem you'll run into here) you bypass the brokered permissions and the load fails since you don't have direct permission to the file itself. I blogged about this several years ago at Skip the path: stick to the StorageFile

    Tuesday, May 29, 2018 11:38 PM
  • Hi Rob,

    Your blog has really very good explanation. 

    I understood the way of BitmapImage.SetSourceAsync and tried the given sample as well. I have one question here. 

    When we try using from streamed data, it is also like a copying the file and reading it. Right?

    Then, if path exists in StorageFile, and user has given the permission as well using fileopenpicker, then framework should allow me to directly load the image using path, but why doesn't it allow?

    P.S I might have missed something to understand in your blog details as I am still trying to understand this concept in depth. 

    Thanks.

    Wednesday, May 30, 2018 5:59 AM
  • The app doesn't have access to the path on the file system. The StorageFile works with a broker that does have access to open the file and return a stream. The implementation is a bit more complicated and changes with OS version. Ultimately the basic file system API will be plumbed through the broker and direct path access will be enabled, but that's not yet the case on the OSes that you're targeting.

    If you need more help with this then please work with your management to open a support case and somebody can explain it to you one on one.

    --Rob

    Wednesday, May 30, 2018 4:53 PM
  • Thanks Rob for the detailed answer!

    It was helpful for me to understand the concept in depth.

    Thanks again.

    Thursday, May 31, 2018 6:50 AM