sme
sme

Reputation: 4153

UWP - How to read .jpg from Desktop inside of app

The functionality I am needing for my app is that if a user copies an image file (for example, a .jpg file in their Desktop folder), I need to extract that file from the clipboard and display it in my app.

I can already extract the storage file from the clipboard, and get the file path. But when I try to read the file path into a BitmapImage, I received a System.UnauthorizedAccessException error, because the image is not located in the app's folder.

Upvotes: 0

Views: 153

Answers (2)

Jet  Chopper
Jet Chopper

Reputation: 1488

You may x:Bind path to XAML Image

    <Image>
        <Image.Source>
            <BitmapImage UriSource="{x:Bind GetAbsoluteUri(Path)}"/>
        </Image.Source>
    </Image>

Code behind:

    private Uri GetAbsoluteUri(string path)
    {
        return new Uri(Path.GetFullPath(path), UriKind.Absolute);
    }

Try, hope it'll work :)

Upvotes: 0

Sunny
Sunny

Reputation: 1534

It sounds like you are trying to open the file by path rather than directly using the StorageFile returned by the file picker.

Your app doesn't have direct access to most of the file system (including the downloads directories) and can access such files only indirectly via the file system broker. The StorageFile object works with the broker to open files the use had authorized and provides a stream of the files contents for the app to read and write to.

You can use Xamarin.Plugin.FilePicker or FilePicker-Plugin-for-Xamarin-and-Windows .

Upvotes: 2

Related Questions