2013-01-23 60 views
0

在我的應用程序中,我必須從遠程服務器下載所有圖像並將其顯示在列表視圖中。當我試圖從獨立存儲中提取圖像時已經有一個例外拋出異常「IsolatedStorageFileStream不允許操作」當從isloated存儲中讀取

「不允許操作上IsolatedStorageFileStream」

。這裏是我的代碼

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

上述功能用於從獨立存儲得到位圖圖像,和我的模型類是

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

和我使用的是singlton類調用函數來獲取圖像。

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

這是設置圖像爲對象的代碼

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

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

    } 

請任何一個幫助我一個解決方案。

+0

似乎有太多的代碼,你正面臨的異常......至少添加評論,你會得到一個異常,並刪除瘋狂的空行數量... –

+0

在哪一行你會得到錯誤? –

+0

@MattLacey使用(IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath,FileMode.Open,FileAccess.Read))/ /這裏的豁免發生 – Sujiz

回答

0

如果您尚未關閉之前打開的流到同一文件,通常會發生此異常。檢查CacheImageFileConverter.DownloadFromWeb並確保您的輸出流包裹在using塊中。

這個例外可能如果該文件不存在也會被拋出,但我不是100%確定。也許通過打電話給IsolatedStorageFile.FileExists(),然後再打開它才能確定。

相關問題