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 に答える