locked
Download a jpeg file and store it in the LocalFolder RRS feed

  • Question

  • How do I download and store a jpg image from the internet in a Windows Store App?

    The problem that I am facing is that I don't know what Get…Async and Write…Async method I must use for an image?

    …    
    using Windows.Storage;
    using Windows.Web.Http;
    
    Uri uri = new Uri("http://image.tmdb.org/t/p/w300/" + posterPath);
    HttpClient httpClient = new HttpClient();
    
    // I guess I need to use one of the Get...Async methods?
    var image = await httpClient.Get…Async(uri);
    
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
    StorageFolder cachedPostersFolder = await localFolder.CreateFolderAsync("cached posters", CreationCollisionOption.OpenIfExists);
    
    StorageFile posterFile = await cachedPostersFolder.CreateFileAsync(posterPath, CreationCollisionOption.ReplaceExisting);
    
    // I guess I need to use one of the Write...Async methods?
    await FileIO.Write…Async(posterFile, image);

    Sunday, November 2, 2014 8:55 PM

Answers

  • Right, here you go:

    Uri uri = new Uri("http://image.tmdb.org/t/p/w300/" + posterPath);
    
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFolder cachedPostersFolder = await localFolder.CreateFolderAsync("cached posters", CreationCollisionOption.OpenIfExists);
    
                StorageFile posterFile = await cachedPostersFolder.CreateFileAsync(posterPath, CreationCollisionOption.ReplaceExisting);
                
                Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
                var image = await httpClient.GetAsync(uri);
                
                using (IInputStream inputStream = await image.Content.ReadAsInputStreamAsync())
                {
                    using (IOutputStream outputStream = await posterFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        await RandomAccessStream.CopyAndCloseAsync(inputStream, outputStream);
                    }
                }
    
    Please remember to mark all helpful posts as answer and/or helpful.
    • Marked as answer by ϻοϰsϯεr Sunday, November 2, 2014 10:27 PM
    Sunday, November 2, 2014 10:23 PM

All replies

  • You could use the GetStreamAsync method and the OpenStreamForWriteAsync() method like this:

     
               Uri uri = new Uri("http://image.tmdb.org/t/p/w300/" + posterPath);
                HttpClient httpClient = new HttpClient();
    
                // I guess I need to use one of the Get...Async methods?
                var image = await httpClient.GetStreamAsync(uri);
    
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFolder cachedPostersFolder = await localFolder.CreateFolderAsync("cached posters", CreationCollisionOption.OpenIfExists);
    
                StorageFile posterFile = await cachedPostersFolder.CreateFileAsync(posterPath, CreationCollisionOption.ReplaceExisting);
    
                using (var fileStream = await posterFile.OpenStreamForWriteAsync())
                {
                    await image.CopyToAsync(fileStream);
                }
    

    Please remember to mark helpful posts as answer and/or helpful.
    Sunday, November 2, 2014 9:08 PM
  • Please notice, I am using the new Windows.Web.Http.HttpClient and GetStreamAsync() is not defined.
    Sunday, November 2, 2014 9:13 PM
  • Right, here you go:

    Uri uri = new Uri("http://image.tmdb.org/t/p/w300/" + posterPath);
    
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFolder cachedPostersFolder = await localFolder.CreateFolderAsync("cached posters", CreationCollisionOption.OpenIfExists);
    
                StorageFile posterFile = await cachedPostersFolder.CreateFileAsync(posterPath, CreationCollisionOption.ReplaceExisting);
                
                Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
                var image = await httpClient.GetAsync(uri);
                
                using (IInputStream inputStream = await image.Content.ReadAsInputStreamAsync())
                {
                    using (IOutputStream outputStream = await posterFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        await RandomAccessStream.CopyAndCloseAsync(inputStream, outputStream);
                    }
                }
    
    Please remember to mark all helpful posts as answer and/or helpful.
    • Marked as answer by ϻοϰsϯεr Sunday, November 2, 2014 10:27 PM
    Sunday, November 2, 2014 10:23 PM
  • Oh god, thank you. It took me countless hours to get this working. You must be the only person on the internet who knows how to actually do this with only using Windows.Web.Http!!!
    Sunday, November 2, 2014 10:28 PM