【问题标题】:Throws Exception "Operation not permitted on IsolatedStorageFileStream" When read from isloated storage从孤立存储读取时引发异常“在独立存储文件流上不允许操作”
【发布时间】:2013-01-23 17:51:47
【问题描述】:

在我的应用程序中,我必须从远程服务器下载所有图像并将其显示在列表视图中。当我尝试从隔离存储中提取图像时出现异常

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

我正在使用单例类来调用函数来获取图像。

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

    }

请任何人帮助我解决问题。

【问题讨论】:

  • 您面临的异常似乎有太多代码...至少在您遇到异常的地方添加注释并删除大量空行...
  • 你在哪一行得到错误?
  • @MattLacey using (IsolatedStorageFileStream sourceFile = _storage.OpenFile(isolatedStoragePath, FileMode.Open, FileAccess.Read))//这里出现异常
  • 仅供参考,“单身人士”是一个词而不是两个词。对于非泛型类来说,这也是一个糟糕的名称,因为它没有描述类的作用(除了它只有一个实例)
  • 另外,您对sourceFile.Close() 的调用是多余的,因为它将作为using 块的一部分被关闭。

标签: c# windows-phone-7 windows-phone-7.1.1


【解决方案1】:

如果您没有关闭以前打开的流到同一文件,通常会发生此异常。检查CacheImageFileConverter.DownloadFromWeb 并确保您的输出流包含在using 块中。

如果文件不存在,可能也会抛出异常,但我不能 100% 确定。也许在你打开它之前打电话给IsolatedStorageFile.FileExists() 只是为了确定。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多