Saving a stream containing an image to Local folder on Windows Phone 8

后端 未结 1 1962
走了就别回头了
走了就别回头了 2021-01-31 12:36

I\'m currently trying to save an stream containing a jpeg image I got back from the camera to the local storage folder. The files are being created but unfortunately contain no

相关标签:
1条回答
  • 2021-01-31 13:21

    Your method SaveToLocalFolderAsync is working just fine. I tried it out on a Stream I passed in and it copied its complete contents as expected.

    I guess it's a problem with the state of the stream that you are passing to the method. Maybe you just need to set its position to the beginning beforehand with file.Seek(0, SeekOrigin.Begin);. If that doesn't work, add that code to your question so we can help you.

    Also, you could make your code much simpler. The following does exactly the same without the intermediate classes:

    public async Task SaveToLocalFolderAsync(Stream file, string fileName)
    {
        StorageFolder localFolder = ApplicationData.Current.LocalFolder;
        StorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
        using (Stream outputStream = await storageFile.OpenStreamForWriteAsync())
        {
            await file.CopyToAsync(outputStream);
        }
    }
    
    0 讨论(0)
提交回复
热议问题