0

IsolatedStorageFileStream我的WP7 项目中有一个非常奇怪的问题。我总是收到类似“ Operation not allowed on IsolatedStorageFileStream ”的错误。

这是我的代码(2个案例):

1º:

void camera_Completed(object sender, PhotoResult e)
{
    var imageBytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!myIsolatedStorage.DirectoryExists("Fotos"))
            myIsolatedStorage.CreateDirectory("Fotos");


         IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile("foto." + DateTime.Now.Date + "jpeg");

        BitmapImage bitmap = new BitmapImage();

          bitmap.SetSource(e.ChosenPhoto);
        WriteableBitmap wb = new WriteableBitmap(bitmap);
        Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
        fileStream.Close();
    }

    e.ChosenPhoto.Seek(0, SeekOrigin.Begin);
    imgField.Source = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

    thumbnail = imageBytes;
    base64String = System.Convert.ToBase64String(imageBytes, 0, imageBytes.Length);
}

2º:

void camera_Completed(object sender, PhotoResult e)
{
    var imageBytes = new byte[e.ChosenPhoto.Length];
    e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);

    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (!myIsolatedStorage.DirectoryExists("Fotos"))
            myIsolatedStorage.CreateDirectory("Fotos");


        IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(@"Shared/foto." + DateTime.Now.Date + "jpeg", FileMode.CreateNew, myIsolatedStorage);

        BitmapImage bitmap = new BitmapImage();

        bitmap.SetSource(e.ChosenPhoto);
        WriteableBitmap wb = new WriteableBitmap(bitmap);
        Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
        fileStream.Close();
    }

    e.ChosenPhoto.Seek(0, SeekOrigin.Begin);
    imgField.Source = PictureDecoder.DecodeJpeg(e.ChosenPhoto);

    thumbnail = imageBytes;
    base64String = System.Convert.ToBase64String(imageBytes, 0, imageBytes.Length);
}

谁能告诉为什么此代码不起作用,并且该站点的示例有效: http: //www.windowsphonegeek.com/tips/All-about-WP7-Isolated-Storage---Read-and-Save-Captured-图片

4

1 回答 1

0

来自 IsolatedStorage 的错误消息非常有用。例如,如果您尝试打开一个不存在的文件,您将收到同样的消息。如果不知道您在哪里得到异常,很难猜测代码中的确切问题是什么,但如果我不得不猜测,我会说原因是试图生成这样的文件名:

@"Shared/foto." + DateTime.Now.Date + "jpeg"

我不确定日期是如何转换为字符串的,这取决于语言环境,但我的猜测是它没有创建有效的文件名。

于 2012-05-23T14:20:08.550 回答