2016-07-30 225 views
3

我试图将文件保存到使用这些方法的应用程序存储(应用程序运行时):UWP无法保存文件

首先,我从一个URI创建文件:

StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(name, uri, RandomAccessStreamReference.CreateFromUri(uri)); 

然后我将文件复制到应用程序的存储以便在将来使用它:

StorageFile file2 = await file.CopyAsync(ApplicationData.Current.LocalFolder, name, NameCollisionOption.ReplaceExisting); 

但第二种方法CopyAsync()没有完成,然后随机辉ls在等待〜5min后出现此错误:

Error screenshot

另外,进程在应用程序的生命周期的每个我称之为T时间他CopyAsync加()方法

Lifecycle processes

当我点击其中一个进程时,CopyAsync()方法结束。

要解决此问题,我必须重新启动手机并卸载/安装应用程序。

注意:当我在后台任务中尝试此操作时,100%的时间都会失败。

+0

完整的项目位于[我的github回购](https://github.com/rootasjey/citations365) –

回答

3

我能发出感谢解决,以this post

和代码在后台任务的工程,以及:)

这并不能解释为什么FileStorage.CopyAsync()方法失败,但这是一种解决方法,直到有人获得更多信息。

(这似乎当CopyAsync()在后台任务失败,它会阻止在后台任务或在前台应用程序的任何未来的危机调用此方法)

的代码片段:

private async Task<StorageFile> DownloadImagefromServer(string URI, string filename) { 
     filename += ".png"; 
     var rootFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Citations365\\CoverPics", CreationCollisionOption.OpenIfExists); 
     var coverpic = await rootFolder.CreateFileAsync(filename, CreationCollisionOption.FailIfExists); 

     try { 
      HttpClient client = new HttpClient(); 
      byte[] buffer = await client.GetByteArrayAsync(URI); // Download file 
      using (Stream stream = await coverpic.OpenStreamForWriteAsync()) 
       stream.Write(buffer, 0, buffer.Length); // Save 

      return coverpic; 
     } catch { 
      return null; 
     } 
}