[英]Saving an image from the web to the Saved Pictures folder in Windows Phone 8.1 RT C#
How can I save an image to the Saved Pictures folder in Windows Phone 8.1 RT? 如何将图像保存到Windows Phone 8.1 RT中的“已保存的图片”文件夹? I downloaded the image from the web using HttpClient.
我使用HttpClient从网上下载了图像。
You just need this 你只需要这个
var url = "Some URL";
var fileName = Path.GetFileName(url.LocalPath);
var thumbnail = RandomAccessStreamReference.CreateFromUri(url);
var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(fileName, url, thumbnail);
await remoteFile.CopyAsync(KnownFolders.SavedPictures, fileName, NameCollisionOption.GenerateUniqueName);
You need to set the Pictures Library capability in your manifest. 您需要在清单中设置图片库功能。
I solved it thanks to mSpot Inc on the MSDN-forums. 感谢MSDN论坛上的mSpot Inc,我解决了这个问题。 The code I use now:
我现在使用的代码:
StorageFolder picsFolder = KnownFolders.SavedPictures;
StorageFile file = await picsFolder.CreateFileAsync("myImage.jpg", CreationCollisionOption.GenerateUniqueName);
string url = "http://somewebsite.com/someimage.jpg";
HttpClient client = new HttpClient();
byte[] responseBytes = await client.GetByteArrayAsync(url);
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
using (var outputStream = stream.GetOutputStreamAt(0))
{
DataWriter writer = new DataWriter(outputStream);
writer.WriteBytes(responseBytes);
writer.StoreAsync();
outputStream.FlushAsync();
}
If you want the user to pick for the file location you need to use the FileSavePicker with pictureLibrary as the SuggestedStartLocation . 如果您希望用户选择文件位置,您需要使用带有pictureLibrary的FileSavePicker作为SuggestedStartLocation 。
If you want to save it without the user to select the destination you need to use something like this: 如果您想在没有用户选择目的地的情况下保存它,您需要使用以下内容:
Windows.Storage.KnownFolders.picturesLibrary.createFileAsync
I believe in both cases you need to set the Pictures Library
capability in your manifest. 我相信在这两种情况下,您都需要在清单中设置
Pictures Library
功能 。
At the moment, I use this native implementation: 目前,我使用这个原生实现:
var url = new Uri(UriString, UriKind.Absolute);
var fileName = Path.GetFileName(url.LocalPath);
var w = WebRequest.CreateHttp(url);
var response = await Task.Factory.FromAsync<WebResponse>(w.BeginGetResponse, w.EndGetResponse, null);
await response.GetResponseStream().CopyToAsync(new FileStream(ApplicationData.Current.LocalFolder.Path + @"\" + fileName, FileMode.CreateNew));
var file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
await file.CopyAsync(KnownFolders.SavedPictures, fileName, NameCollisionOption.FailIfExists);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.