Search code examples
c#imagewindows-phonewindows-phone-8.1isolatedstorage

WP 8.1 - How to save an image to Isolated Storage if know it's URL


I have the image URL.

How can I save it into Isolated Storage in WP 8.1.

Can I trigger both save it and then share it onto Facebook with only one button?

This is my code - which work well following Burak Kaan Köse's:

public async void GetImage()
    {
        StorageFolder folder = ApplicationData.Current.LocalFolder;
            if (folder != null)
            {
                StorageFile file = await folder.CreateFileAsync("imagefile", CreationCollisionOption.ReplaceExisting);

                string url = imgUri[fLFl.SelectedIndex].ToString();
                HttpClient client = new HttpClient();
                byte[] fileContent = await client.GetByteArrayAsync(url); ; // This is where you set your content as byteArray

                Stream fileStream = await file.OpenStreamForWriteAsync();
                fileStream.Write(fileContent, 0, fileContent.Length);
                fileStream.Flush();
                fileStream.Dispose();
            }
    }

Solution

  • Don't forget to change 'imagefile' path and fileContent variable.

    private async void SaveFile()
    {
        try
        {
            StorageFolder folder = ApplicationData.Current.LocalFolder;
            if(folder != null)
            {
                StorageFile file = await folder.CreateFileAsync("imagefile", CreationCollisionOption.ReplaceExisting);
                byte[] fileContnet = null; // This is where you set your content as byteArray
                Stream fileStream = await file.OpenStreamForWriteAsync();
                fileStream.Write(fileContent, 0, fileContent.Length);
                fileStream.Flush();
                fileStream.Close();
            }
        }
        catch (Exception ex)
        {
            // Some Exception handling code
        }
    }