Saving picture from internet to Saved Photos folder in Windows Phone 8.1 RT C #
4 answers
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 Image Library feature in your manifest.
+3
source to share
I solved it thanks to mSpot Inc on the MSDN forums. The code I'm using 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();
}
+1
source to share
If you want the user to choose the location of the file, you need to use FileSavePicker with pictureLibrary as SuggestedStartLocation .
If you want to save it without a user, in order to select a destination, you need to use something like this:
Windows.Storage.KnownFolders.picturesLibrary.createFileAsync
I believe that in both cases, you need to set the Pictures Library
capability in your manifest.
0
source to share
I am currently using this built-in 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);
0
source to share