0

I am getting a file, via a picker and I am displaying that image in a Image control in xaml.

The image displays perfectly, but when I tried to convert the path to bytes, I get the error

Tried to give picture library permition

  var picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpeg");

            var file = await picker.PickSingleFileAsync();

            if (file != null) {

                var stream =  await file.OpenReadAsync();

                var bitmap = new BitmapImage();

                bitmap.SetSource(stream);

                var bytes = File.ReadAllBytes(file.Path); // Error

                selectedimage.Source = bitmap;

{"Access to the path 'some path' is denied."}

  • Thank you @PavelAnikhouski, but I already tried it and it docent work. Besides, what I want to do is convert it into bytes. The image will display, but the app crash on File.ReadAllBytes(file.Path) –  Aug 13 '19 at 11:15
  • in UWP you should use methods from `FileIO` class – Pavel Anikhouski Aug 13 '19 at 12:00
  • Thanks @PavelAnikhouski, but I wanted to read the bytes –  Aug 13 '19 at 12:12
  • 1
    This is already answered here [thread](https://stackoverflow.com/questions/36081740/uwp-async-read-file-into-byte) `File.ReadAllBytes` isn't for UWP – Pavel Anikhouski Aug 13 '19 at 12:44

1 Answers1

0

Access to the path, when try to ReadAllTheBytes

UWP run in sandbox, we can't access the file with the path directly,Even though we already add broadFileSystemAccess capability, we could not use ReadAllTheBytes under System.IO namespace. And Pavel Anikhouski's comment is correct, we could use MemoryStream to convert the file to bytes.

    byte[] result;
    using (Stream stream = await imageFile.OpenStreamForReadAsync())
    {
        using (var memoryStream = new MemoryStream())
        {

            stream.CopyTo(memoryStream);
            result = memoryStream.ToArray();
        }
    }
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36