Beispiel #1
0
        public async Task<IEnumerable<FlickrPhoto>> GetRecentlyAddedPhotosAsync(int? numberOfPhotos = 100)
        {
            IEnumerable<FlickrPhoto> flickrPhotos = new FlickrPhoto[0];
            try
            {
                var command = new GetRecentFlickrPhotosCommand(_apiKey);
                command.AddParameter("per_page", (numberOfPhotos ?? 100).ToString());
                command.AddParameter("extras", "owner_name,date_upload,original_format,date_taken");

                var result = await _restClient.ExecuteAsync(command);
                flickrPhotos = FlickrHelpers.CreateFlickrPhotoFromXml(result.Data.Root);
            }
            catch (Exception e)
            {
                OnApiErrorOccured(new ApiErrorOccuredEventArgs(e) { ErrorMessage = e.Message });
            }
            return flickrPhotos;
        }
        public async Task SaveFlickrPhotoToPictureLibraryAsync(FlickrPhoto photo)
        {
            using (var client = new HttpClient())
            {
                var image = await client.GetByteArrayAsync(photo.LargeImageUrl);
                var localFile = await KnownFolders.PicturesLibrary.CreateFileAsync(GetFilenameFromUrl(photo.LargeImageUrl), CreationCollisionOption.GenerateUniqueName);

                using (var fileStream = await localFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    using (var writeStream = fileStream.GetOutputStreamAt(0))
                    {
                        using (var writer = new DataWriter(writeStream))
                        {
                            writer.WriteBytes(image);
                            await writer.StoreAsync();
                            writer.DetachStream();
                        }
                        await writeStream.FlushAsync();
                    }
                }
            }
        }