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.
sourceFile.Close()
is redundant as it will be closed as part of theusing
block. – Richard Szalay