【问题标题】:how to get Bytes of a file for sending in UWP?如何获取要在 UWP 中发送的文件字节数?
【发布时间】:2018-10-02 11:29:27
【问题描述】:

我是 UWP 的新手,我想打开任何类型的文件并将其字节传输给接收者。例如对于 jpg 文件,我编写了以下代码:

// Create FileOpenPicker instance    
FileOpenPicker fileOpenPicker = new FileOpenPicker();

// Set SuggestedStartLocation    
fileOpenPicker.SuggestedStartLocation =         PickerLocationId.PicturesLibrary;

// Set ViewMode    
fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
fileOpenPicker.FileTypeFilter.Clear();
fileOpenPicker.FileTypeFilter.Add(".jpg");

// Open FileOpenPicker    
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
byte[] bytesRead = File.ReadAllBytes(file.Path);

 string  Paths = 
 @"C:\\Users\zahraesm\Pictures\sample_reconstructed.jpg";
 File.WriteAllBytes(Paths, bytesRead);           

最后两行用于将字节写入假设在接收器中的文件。但是我不断收到以下异常:

System.InvalidOperationException: '不应在 UI 线程上执行同步操作。考虑将此方法包装在 Task.Run 中。'

【问题讨论】:

  • 请说明您在哪一行得到此异常?
  • 我在线收到错误:File.ReadAllBytes(file.Path)

标签: c# uwp uwp-xaml fileopenpicker


【解决方案1】:

试试这个代码。

try {
            FileOpenPicker openPicker = new FileOpenPicker {
                ViewMode = PickerViewMode.Thumbnail,
                SuggestedStartLocation = PickerLocationId.DocumentsLibrary,
                FileTypeFilter = { ".jpg", ".jpeg", ".png" }
            };

            StorageFile file = await openPicker.PickSingleFileAsync();
            if (file != null) {
                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read)) {
                    var reader = new Windows.Storage.Streams.DataReader(fileStream.GetInputStreamAt(0));

                    var LoadReader = await reader.LoadAsync((uint)fileStream.Size);

                    byte[] pixels = new byte[fileStream.Size];
                    reader.ReadBytes(pixels);
                }
            }
        } catch (Exception ex) {

        }

【讨论】:

    【解决方案2】:

    考虑将最后一个操作包装在 Task.Run()

    await Task.Run(()=>{
        byte[] bytesRead = File.ReadAllBytes(file.Path);        
        string  Paths = 
        @"C:\\Users\zahraesm\Pictures\sample_reconstructed.jpg";
        File.WriteAllBytes(Paths, bytesRead); 
    });
    

    【讨论】:

      【解决方案3】:

      您应该直接从您的 FilePicker 返回的 StorageFile 中读取字节,以免您将来遇到文件权限错误。

      StorageFile file = await fileOpenPicker.PickSingleFileAsync();
      var buffer = await FileIO.ReadBufferAsync(file);
      byte[] bytes = System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.ToArray(buffer);
      

      你也应该使用await FileIO.WriteBytesAsync(targetFile, myBytes)来写。

      除非您的包清单中有broadFileSystemAccess,否则您通常应避免使用System.IO API,除非您知道您的应用程序明确有权访问该区域(即您的应用程序的本地存储)中的文件,而是使用Windows.Storage API 的

      查看 MSDN 以获取 File Access Permissions for UWP apps 以获取有关文件权限的更多信息。

      如果您确实使用System.IO,请始终通过await Task.Run(() => { ... } 在后台线程上执行工作

      【讨论】:

        猜你喜欢
        • 2014-06-06
        • 1970-01-01
        • 2015-02-18
        • 2017-10-16
        • 1970-01-01
        • 2012-08-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多