How to display image form local forder uwp

Viewed 1262

I'm trying to visualize automatically (without user interaction) an image saved in C:\Myfolder\image.jpg in a UWP app. I know that this is a hot topic and UWP app can access only some "parts" of the filesystem but I also read that there are different solutions to overcome this problem like this

Trying this: mainpage.cs

BitmapImage bitmap = new BitmapImage();
StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(@"C:\MyFolder");
StorageFile file = await folder.GetFileAsync("image.jpg");
using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
     bitmap.SetSource(stream);
     image0.Source = bitmap;
}

adding also this lines in appxmanifest:

<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="App">
<Extensions>
    <uap:Extension Category="windows.fileTypeAssociation">
      <uap:FileTypeAssociation Name="jpegfiles">
        <uap:SupportedFileTypes>
          <uap:FileType>.jpg</uap:FileType>
        </uap:SupportedFileTypes>
      </uap:FileTypeAssociation>
    </uap:Extension>
  </Extensions>
  </Application>
 </Applications>
 <Capabilities>
   <Capability Name="internetClient" />
   <Capability Name="privateNetworkClientServer" />
   <Capability Name="internetClientServer" />
   <uap:Capability Name="enterpriseAuthentication" />
   <uap4:Capability Name="userDataTasks" />
   <DeviceCapability Name="microphone" />
 </Capabilities>

Unfortunately, I'm obtaining always an error like: System.UnauthorizedAccessException: 'Access is denied.

I also tried to wirte the direct path of the image inside the Image object something like:

 image0.Source = new BitmapImage(new Uri(@"C:\Myfolder\image.jpg"));

and in this case the app starts but it does not show anything.

Maybe I missed something or I do not understand how to set File access permissions.

2 Answers

There are actually ways to access local files anywhere on the disk in UWP now. You can declare the restricted broadFileSystemAccess capability (see docs), which will allow you to access any path on the disk using the StorageFile APIs (note that you still can't access those paths using classic System.IO APIs). If you enable broadFileSystemAccess your code will start working as is.

However, to declare this capability, your app needs to have a good reason as it is a potential security threat to the user's device. Docs state:

This is a restricted capability. Access is configurable in Settings > Privacy > File system. Because users can grant or deny the permission any time in Settings, you should ensure that your app is resilient to those changes. If you find that your app does not have access, you may choose to prompt the user to change the setting by providing a link to the Windows 10 file system access and privacy article. Note that the user must close the app, toggle the setting, and restart the app. If they toggle the setting while the app is running, the platform will suspend your app so that you can save the state, then forcibly terminate the app in order to apply the new setting. In the April 2018 update, the default for the permission is On. In the October 2018 update, the default is Off.

If you submit an app to the Store that declares this capability, you will need to supply additional descriptions of why your app needs this capability, and how it intends to use it. This capability works for APIs in the Windows.Storage namespace. See the Example section at the end of this article for an example of how to enable this capability in your app.

There are however locations like ApplicationData.Current.LocalFolder or ApplicationData.Current.RoamingFolder which you can access without additional permissions. You can also declare capabilities like musicLibrary, etc. to access libraries.

Finally, you can also let the user pick the file manually using a dialog utilizing classes like FileOpenPicker or FileSavePicker and then persist the granted access to this file using StorageApplicationPermissions.FutureAccessList - read more about this here.

For extensive information see the docs.

You cannot directly access files like that in UWP. The real problem is that path your using is protected. You need to work in the folders that UWP gives your app access too.

You can get much more information at:

https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files

You will want to do something like the following:

Windows.Storage.StorageFolder storageFolder =
    Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =
    await storageFolder.GetFileAsync("your.bmp");

Then you can start working with the file.

The following link also shows how to enumerate files in your Pictures folder: https://docs.microsoft.com/en-us/windows/uwp/files/quickstart-listing-files-and-folders

Related