Windows phone 7 尝试创建任何类型的文件时发出IsolatedStorageFileStream
在我的WP7项目中,Windows phone 7 尝试创建任何类型的文件时发出IsolatedStorageFileStream,windows-phone-7,Windows Phone 7,在我的WP7项目中,IsolatedStorageFileStream有一个非常棘手的问题。我总是会遇到类似“操作不允许在IsolatedStorageFileStream上进行”这样的错误 这是我的代码(2个案例): 1º: void camera_Completed(object sender, PhotoResult e) { var imageBytes = new byte[e.ChosenPhoto.Length]; e.ChosenPhoto.Read(imageB
IsolatedStorageFileStream
有一个非常棘手的问题。我总是会遇到类似“操作不允许在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);
}
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);
}
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 = 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);
}
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);
}
有人能告诉我为什么这段代码不起作用吗?这个网站的一个例子是这样的:?来自IsolatedStorage的错误消息非常有用。例如,如果您试图打开一个不存在的文件,您将收到相同的消息。在不知道异常发生在哪里的情况下,很难猜测代码中的确切问题是什么,但如果必须猜测,我会说原因是试图生成如下文件名:
@"Shared/foto." + DateTime.Now.Date + "jpeg"
我不确定日期是如何转换为字符串的,这取决于语言环境,但我猜它没有创建有效的文件名。您已经给出了很多代码-哪个方法调用失败?生成的文件名是什么?它是否有任何无效字符?失败的方法调用是此行IsolatedStorageFileStream fileStream=new IsolatedStorageFileStream(@“Shared/foto.”+DateTime.Now.Date+“jpeg”、FileMode.CreateNew、myIsolatedStorage);但是在重载方法中使用字符串名称之前,我成功地连接了字符串名称,我想这是WP7编程的问题吧?最后它工作得很好:),感谢大家的回答^^实际上它创建了一个有效的文件名,但它没有将日期与字符串的其余部分连接起来,最后它在方法中使用它之前构建了字符串文件名。