1

我有一个应用程序需要下载并保存设备上的文件 - 视频。Windows Phone IsolatedStorage

视频短~10分钟,质量差,这意味着它们的大小是最小的。

所以,问题是,当我下载一些文件 - 一切顺利,但一些文件失败,错误: 内存不足的异常。从逻辑上讲,我认为小于一定大小(例如50MB)的文件可以很好地下载,但更高 - 异常。

这里是我的代码:

异常详细信息:

System.OutOfMemoryException was unhandled Message: An unhandled exception of type 'System.OutOfMemoryException' occurred in System.Windows.ni.dll

异常图像:

enter image description here

对此有一个解决方法吗?

回答

0

当响应完全加载时,它完全驻留在内存中。这导致您的OutOfMemoryException。解决方案是将响应直接“流”到独立存储中。

请注意,下面的解决方案目前的缺点是您正在失去下载进度信息。

public async void btnDownload2_Click() 
{ 
    try 
    { 
    var httpClient = new HttpClient(); 
    var response = await httpClient.GetAsync(new Uri("http://somelink/video/nameOfFile.mp4"), HttpCompletionOption.ResponseHeadersRead); 

    response.EnsureSuccessStatusCode(); 

    using(var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
     bool checkQuotaIncrease = IncreaseIsolatedStorageSpace(e.Result.Length); 

     string VideoFile = "PlayFile.wmv"; 
     using(var isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile)) 
     { 
     using(var stm = await response.Content.ReadAsStreamAsync()) 
     { 
      stm.CopyTo(isolatedStorageFileStream); 
     } 
     } 
    } 
    } 
    catch(Exception) 
    { 
    // TODO: add error handling 
    } 
} 
+0

但HTTP客户端仅适用于4.5+框架和Windows Phone 8 但我的应用程序需要在WP7 + – Cheese

+0

所以正确的问题将被移植,我怎么能实现直接保存到隔离来自WebClient或HTTP客户端的存储 – Cheese

+0

WP7.5支持HttpClient。甚至包括异步/等待支持。看到这个页面:http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx –

相关问题