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

499 views Asked by At

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();
            }
    }
1

There are 1 answers

2
Burak Kaan Köse On BEST ANSWER

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
    }
}