异常在Windows商店应用中显示下载的图像



嗨,我正在从以下代码下载图像

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, imageUri);
            HttpResponseMessage response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
            var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite);
            DataWriter writer = new DataWriter(fs.GetOutputStreamAt(0));
            writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
            await writer.StoreAsync();
            writer.DetachStream();
            await fs.FlushAsync();
            fs.Dispose();

但是,当我尝试在运行下载代码后立即打开它时。在以下代码的帮助下,并尝试将其设置为映像控制

            var imageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(imageName);
            IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read);
            imageControl.SetSource(stream);

它引发以下异常

访问被拒绝。(Hresult的例外:0x80070005(e_accessdenied))

和内部异常是:

null

请让我知道,在这一点上我犯错。

预先感谢

在这里得到答案。

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, imageUri);
            HttpResponseMessage response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
            var imageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
            using (var fs = await imageFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                var outStream = fs.GetOutputStreamAt(0);
                DataWriter writer = new DataWriter(outStream);
                writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
                await writer.StoreAsync();
                writer.DetachStream();
                await fs.FlushAsync();
                outStream.Dispose();
                fs.Dispose();
            }

相关内容

  • 没有找到相关文章

最新更新