错误保存文件Windows Iot Core jpg格式

本文关键字:jpg 格式 Core Iot 保存文件 Windows 错误 | 更新日期: 2023-09-27 18:02:17

我已经编写了下面的代码:private async void WriteData(){var removableDevices = KnownFolders.RemovableDevices;var externalDrives = await removableDevices.GetFoldersAsync();var drive0 = externalDrives[0];

            var testFolder = await drive0.CreateFolderAsync("test");
            var testFile = await testFolder.CreateFileAsync("test.jpg");
            var byteArray = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
            using (var sourceStream = new MemoryStream(byteArray).AsRandomAccessStream())
            {
                using (var destinationStream = (await testFile.OpenAsync(FileAccessMode.ReadWrite)).GetOutputStreamAt(0))
                {
                    await RandomAccessStream.CopyAndCloseAsync(sourceStream, destinationStream);
                }
            }
        }
        private async void _timer_Tick(object sender, object e)
        {
            DhtReading reading = new DhtReading();
            int val = this.TotalAttempts;
            this.TotalAttempts++;
            reading = await _dht.GetReadingAsync().AsTask();
            _retryCount.Add(reading.RetryCount);
            this.OnPropertyChanged(nameof(AverageRetriesDisplay));
            this.OnPropertyChanged(nameof(TotalAttempts));
            this.OnPropertyChanged(nameof(PercentSuccess));
        if (reading.IsValid)
        {
            this.TotalSuccess++;
            this.Temperature = Convert.ToSingle(reading.Temperature);
            this.Humidity = Convert.ToSingle(reading.Humidity);
            this.LastUpdated = DateTimeOffset.Now;
            this.OnPropertyChanged(nameof(SuccessRate));
            this.WriteData();

        }
        this.OnPropertyChanged(nameof(LastUpdatedDisplay));
    }

我的代码冻结了,我想知道如何保存存档中的湿度和温度值,这是保存这个应用程序的最好方法吗?

错误保存文件Windows Iot Core jpg格式

因为在您第一次运行代码之后,已经创建了"test"文件夹和"test.jpg",所以当您再次运行代码时,您可能会收到此异常。你可以试试:

var testFolder = await drive0.GetFolderAsync("test");
var testFile = await testFolder.GetFileAsync("test.jpg");

代替

var testFolder = await drive0.CreateFolderAsync("test");
var testFile = await testFolder.CreateFileAsync("test.jpg");

注意:这里我将test.jpg保存在外接SD卡上。如果设备中没有SD卡,代码也会遇到此异常

尝试在您的类顶部,MyClass是容器类的名称

StorageFolder testFolder;
public MyClass()
{
    var removableDevices = KnownFolders.RemovableDevices; 
    var externalDrives = await removableDevices.GetFoldersAsync(); 
    var drive0 = externalDrives[0];
    testFolder = await drive0.CreateFolderAsync("test");
    await testFolder.CreateFileAsync("test.jpg");
}

WriteData方法中,只写

var testFile = await testFolder.GetFolderAsync("test.jpg");
观察

  • WriteData变慢于removableDevices.GetFoldersAsync()
  • 当你使用计时器时,你需要保持Tick事件尽可能快和资源空闲