简体   繁体   English

将图像从Web保存到Windows Phone 8.1 RT C#中的“保存的图片”文件夹

[英]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 . 如果您希望用户选择文件位置,您需要使用带有pictureLibraryFileSavePicker作为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.

相关问题 Windows Phone 8.1 RT在C#中更改图像源 - Image source changing in C# for windows phone 8.1 RT 如何在Windows Phone 8.1 RT中将文件从Assets文件夹导出到手机的Documents文件夹? - How to export a file from the Assets folder to the phone's Documents folder in Windows Phone 8.1 RT? 将图片从Windows Phone 8.1上传到Web - Upload image from WIndows phone 8.1 to web Windows Phone 8.1 RT-保存和加载页面状态 - Windows Phone 8.1 RT - Saving and loading page state 如何在挂起Windows Phone 8.1 rt xaml C时保存ui状态 - how to save ui state on suspending Windows phone 8.1 rt xaml c# 如何从ImageSource对象获取Stream以保存在图片库中? (Windows Phone 7,C#) - How to get Stream from ImageSource object for saving in Pictures Library? (Windows Phone 7, C#) 将列表保存并检索到本地存储中(Windows Phone 8.1&C#) - Saving and retrieving list to a local storage (Windows Phone 8.1 & C#) 如何在Windows Phone 8.1 RT中创建旋转图像滑块? - How to create a rotating image slider in Windows Phone 8.1 RT? 在Windows Phone 8.1 RT上从{*}从语音命令获取文本 - Get text from voice command from {*} on Windows Phone 8.1 RT 没有源Windows Phone 8.1 RT的BitmapImage中的WritableBitmapImage - WritableBitmapImage from BitmapImage with no source Windows Phone 8.1 RT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM