0

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

            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))

内部例外是:

空值

请让我知道我在什么时候做错了。

提前致谢

4

1 回答 1

1

在这里得到答案..只需要再添加一行..现在下载代码是

            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();
            }
于 2012-10-22T07:34:57.573 回答