0
votes

In my app I have to download all the images from the remote server and display it in a list view .When i tried to extract the images from isolated storage I have got an exception

"Operation not permitted on IsolatedStorageFileStream"

.here is my code

public static object ExtractFromLocalStorage(Uri imageFileUri)
    {

        byte[] data;
       string isolatedStoragePath = GetFileNameInIsolatedStorage(imageFileUri);       //Load from local storage
        if (null == _storage)
        {

             _storage = IsolatedStorageFile.GetUserStoreForApplication();
        }
         BitmapImage bi = new BitmapImage();
  //HERE THE EXCEPTION OCCURS
        using (IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))
        {
            data = new byte[sourceFile.Length];

            // Read the entire file and then close it
            sourceFile.Read(data, 0, data.Length);
         // Create memory stream and bitmap
        MemoryStream ms = new MemoryStream(data);

        // Set bitmap source to memory stream
        bi.SetSource(ms);
            sourceFile.Close();


        }
        return bi;
    }

the above function is used to get the bitmap image from isolated storage,and my model class is

public class Details
{ 
  public string id { get; set; }
  public string name { get; set; }
  public Uri imgurl { get; set; }
  [IgnoreDataMember]
  public BitmapImage ThumbImage{get;set;}
}

and i am using a singlton class to call the function to get image.

public class SingleTon
{
    static SingleTon instance = null;

    private SingleTon()
    {

    }

    public static SingleTon Instance()
    {


                if (instance == null)
                {
                    instance = new SingleTon();
                }
                return instance;


    }

    public BitmapImage getImage(Uri uri)
    {
        lock (this)
        {
            BitmapImage image = new BitmapImage();
            image = (BitmapImage)CacheImageFileConverter.ExtractFromLocalStorage(uri);
            return image;
        }
    }
    public void writeImageToIsolatedStorage(Uri imageUri)
    {
        lock (this)
        {
            CacheImageFileConverter.DownloadFromWeb(imageUri);
        }
    }
}

this is the code for set the image to the object

 SingleTon singleton = SingleTon.Instance();
 List<(Details > datalist = new Details()
 foreach (Details items in DataList)
        {

    items.ThumbImage = singleton.getImage(items.imgurl );
    datalist.add(items);    

    }

please any one help me with a solution.

1
There seem to be way too much code for exception you are facing... At least add comment where you get an exception AND remove insane amount of empty lines...Alexei Levenkov
On which line do you get the error?Matt Lacey
@MattLacey using (IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))//here the exeption occursSujiz
FYI, "Singleton" is one word not two. It's also a terrible name for a non-generic class as it doesn't describe what the class does (other than that there's only one instance of it)Richard Szalay
Also, your call to sourceFile.Close() is redundant as it will be closed as part of the using block.Richard Szalay

1 Answers

0
votes

This exception usually occurs if you haven't closed a previously open Stream to the same file. Check CacheImageFileConverter.DownloadFromWeb and make sure your output Stream is wrapped in a using block.

That exception might also be thrown if the file doesn't exist, but I'm not 100% sure. Maybe through in a call to IsolatedStorageFile.FileExists() before you open it just to be sure.