2012-10-22 63 views
0

嗨,我下載從下面的代碼圖像下載的圖像異常,同時顯示在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))

和內部異常是:

請讓我知道,在這一點我做的錯誤。

由於提前

回答

1

Got answer here..只是要增加一個行..現在下載代碼

  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(); 
      } 
相關問題