Question

Update

I added the piece of code/function that normally throws the error.

can someone please help.

Before I begin, I have spent over 20+ hours researching this issue.

I am developing a windows phone app and I keep getting a lot of IsolatedStorage Exceptions, especially "Operation not supported on IsolatedStorageFileStream".

The Scenario

I have an object that has a ProfilePictureUrl as a property, every time I create an instance of the object I download the profile image from the web, then I will store that image to the Isolated storage.

Sample Code

foreach(String url in urls)
{
    var profile = new MyClass()
    {
        ProfilePictureURL = url
    };

    profile.DownloadProfilePictureToLocalStorage(() =>
    {
        completed(profile);
    }, (ex) => { incomplete(ex); });
}

The Code that throws the Exception

if (isoFile.FileExists(saveAs))
    isoFile.DeleteFile(saveAs);

using (var isoFileStream = isoFile.CreateFile(saveAs))
{
    var width = wb.PixelWidth;
    var height = wb.PixelHeight;
    Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
}

This is inside the DownloadImageToIsolatedStorage function in the LocalStorageManager class.

Here is the class that manages my IsolatedStorage storing

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Media.Imaging;

namespace Classes.Managers
{
    public delegate void GetImageCompletedDelegate(BitmapImage bmp);
    public delegate void GetImageNotCompletedDelegate(Exception ex);

    public delegate void SaveImageCompletedDelegate();
    public delegate void SaveImageNotCompletedDelegate(Exception ex);

    public delegate void DeleteImageCompletedDelegate();
    public delegate void DeleteImageNotCompletedDelegate(Exception ex);

    class LocalStorageManager
    {
        private static readonly LocalStorageManager _instance = new LocalStorageManager();

        public static LocalStorageManager Instance
        {
            get
            {
                return _instance;
            }
        }

        private bool m_IsBusy;
        public bool IsBusy
        {
            get { return m_IsBusy; }
            private set { m_IsBusy = value; }
        }

        private void GetImageFromIsolatedStorage(String name, GetImageCompletedDelegate completed, GetImageNotCompletedDelegate notCompleted)
        {
            try
            {
                using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    BitmapImage img = new BitmapImage();
                    if (isoFile.FileExists(name))
                    {
                        using (IsolatedStorageFileStream fileStream = isoFile.OpenFile(name, FileMode.Open, FileAccess.Read))
                        {
                            img.SetSource(fileStream);
                        }
                    }
                    completed(img);
                }
            }
            catch (Exception ex) { notCompleted(ex); }
        }

        public void DownloadImageToIsolatedStorage(String url, string saveAs, SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
        {
            try
            {
                this.IsBusy = true;
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
                httpRequest.BeginGetResponse((callback) =>
                {
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {

                        using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            HttpWebResponse response = (HttpWebResponse)httpRequest.EndGetResponse(callback);

                            var bi = new BitmapImage();
                            bi.SetSource(response.GetResponseStream());
                            var wb = new WriteableBitmap(bi);

                            if (isoFile.FileExists(saveAs))
                                isoFile.DeleteFile(saveAs);

                            using (var isoFileStream = isoFile.CreateFile(saveAs))
                            {
                                var width = wb.PixelWidth;
                                var height = wb.PixelHeight;
                                Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
                            }
                            this.IsBusy = false;
                            completed();
                        }
                    });
                }, null);
            }
            catch (Exception e) { this.IsBusy = false; notCompleted(e); }
        }

        public void MoveFile(String source, String destination)
        {
            using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isoFile.FileExists(source))
                {
                    isoFile.MoveFile(source, destination);
                }
            }
        }

        private void WriteImageStreamToIsolatedStorage(Stream imageStream, string saveAs, SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
        {
            try
            {
                using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isolatedStorage.FileExists(saveAs))
                        isolatedStorage.DeleteFile(saveAs);

                    var fileStream = isolatedStorage.CreateFile(saveAs);
                    imageStream.CopyTo(fileStream);
                    fileStream.Close();
                    /*
                    BitmapImage bmp = null;
                    bmp.SetSource(imageStream);
                    var writeableBMP = new WriteableBitmap(bmp);
                    writeableBMP.SaveJpeg(fileStream, writeableBMP.PixelWidth, writeableBMP.PixelHeight, 0, 100);
                    fileStream.Close();*/
                }
                completed();
            }
            catch (Exception ex) { notCompleted(ex); }
        }

        private void DeleteImageFromIsolatedStorage(string imageName, DeleteImageCompletedDelegate completed, DeleteImageNotCompletedDelegate notCompleted)
        {
            try
            {
                using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (isolatedStorage.FileExists(imageName))
                        isolatedStorage.DeleteFile(imageName);
                    completed();
                }
            }
            catch (Exception e) { notCompleted(e); }
        }

    }
}

Here is the class for my objects

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.IO.IsolatedStorage;
using System.Net;

namespace Classes.Model
{
    public class MyClass : INotifyPropertyChanged
    {
        private string m_ProfilePictureURL;
        public string ProfilePictureURL
        {
            get { return m_ProfilePictureURL; }
            set
            {
                if (m_ProfilePictureURL == value)
                    return;

                m_ProfilePictureURL = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("ProfilePictureURL"));
            }
        }

        [IgnoreDataMember]
        public BitmapImage LocalProfilePicture
        {
            get
            {
                using (IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    try
                    {
                        BitmapImage bitmapImage = new BitmapImage();
                        bitmapImage.SetSource(isoFile.OpenFile("ProfilePic_" + this.UUID, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite));
                        return bitmapImage;
                    }
                    catch (Exception ex)
                    {
                        isoFile.Dispose();
                        return null;
                    }
                }
            }
        }

        public void DownloadProfilePictureToLocalStorage(SaveImageCompletedDelegate completed, SaveImageNotCompletedDelegate notCompleted)
        {
            // Wait till its no longer busy
            while (LocalStorageManager.Instance.IsBusy) ;
            if (!String.IsNullOrWhiteSpace(this.ProfilePictureURL) && !String.IsNullOrWhiteSpace(this.UUID))
            {
                LocalStorageManager.Instance.DownloadImageToIsolatedStorage(this.ProfilePictureURL, "ProfilePic_" + this.UUID, () =>
                {
                    if (PropertyChanged != null)
                        PropertyChanged(this, new PropertyChangedEventArgs("LocalProfilePicture"));
                    completed();
                }, (ex) => { notCompleted(ex); });
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top