イメージをメディア ライブラリに保存しようとしていますが、SavePicture 関数が呼び出されているときに "InvalidOperationException" が発生します。Pictures Hub に保存したい画像ファイルが isolatedstorage に保存されています。コードスニペットは次のとおりです-
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fname, FileMode.Open, FileAccess.Read)) //fname is the filename of the image that is to be saved in the library
{
MediaLibrary mediaLibrary = new MediaLibrary();
Picture pic = mediaLibrary.SavePicture("SavedLogo.jpg", fileStream); //Exception thrown here!
fileStream.Close();
}
}
私が集めたものから、この例外は Zune が実行中に画像ライブラリをブロックすることに関係しています。私はそれを止めており、電話もPCに接続していません。このアプリを物理デバイスでテストすると、アプリがクラッシュするだけですが、「保存された画像」に保存された空白の画像が表示されます。
Webクライアントを使用してURLから画像をダウンロードしていますが、正しいようです。Web クライアントには、画像の URL (openReadAsync) が渡されます。openReadCompleted イベントは次のとおりです。
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
var resInfo = new StreamResourceInfo(e.Result, null);
var reader = new StreamReader(resInfo.Stream);
byte[] contents;
using (BinaryReader bReader = new BinaryReader(reader.BaseStream))
{
contents = bReader.ReadBytes((int)reader.BaseStream.Length);
}
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream stream = isf.CreateFile("example.jpg");
stream.Write(contents, 0, contents.Length);
stream.Close();
}
後でファイルを開こうとすると正常に表示されるため、ファイルが正しく保存されていると思います(つまり、隔離された場所に)。