Reputation: 1825
Hi I am downloading an image from the following code
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();
But when I try it to open immediately after running downloading code.. with the help of the following code and trying to set as a source to image control
var imageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(imageName);
IRandomAccessStream stream = await imageFile.OpenAsync(FileAccessMode.Read);
imageControl.SetSource(stream);
It throws the following exception
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
and Inner Exception is:
null
Please let me know that at which point I am doing mistake.
Thanks in Advance
Upvotes: 0
Views: 290
Reputation: 1825
Got answer here.. just had to add one more line.. now the downloading code is
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();
}
Upvotes: 1