2016-11-18 52 views
0

用户可以拍摄多张照片。这些图像被存储在ApplicationData.Current.LocalFolder:存储文件中的UnauthorizedAccessException .OpenReadAsync

folderBatch = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Batch", CreationCollisionOption.OpenIfExists); 

当用户拍照,这种方法被触发:

public async void GetAndProcessImage() 
    { 
      IBuffer ImageBuffer = null; 

      if (App.settings.ImageSource == MyImageSource.Camera) 
       ImageBuffer = await Camera.TakePhotoAsync(); 

      // store in batch folder 
      IReadOnlyList<StorageFile> listFiles = await folderBatch.GetFilesAsync(); 
      int iNumberOfFiles = listFiles.Count; 
      StorageFile fileTarget = await folderBatch.CreateFileAsync(string.Format("batch{0}.jpg", iNumberOfFiles)); 
      IRandomAccessStream filestream = await fileTarget.OpenAsync(FileAccessMode.ReadWrite); 
      await filestream.GetOutputStreamAt(0).WriteAsync(ImageBuffer); 
      await filestream.FlushAsync(); 
} 

该照片是用这种方法,通过上述GetAndProcessImage称为:

public async Task<IBuffer> TakePhotoAsync() 
    { 
     Debug.WriteLine("taking picture..."); 
     InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); 

     if (mediaCapture.VideoDeviceController.FocusControl.Supported) 
      await mediaCapture.VideoDeviceController.FocusControl.FocusAsync(); 

     try 
     { 
      await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); 
     } 
     catch (Exception e) 
     { 
      ExceptionHandler.Instance.HandleException(e); 
     } 
     IBuffer ibuffer = await StreamHelpers.StreamToIBuffer(stream); 
     return ibuffer; 
    } 

当用户完成时,他/她可以推一推一个按钮开始读取批处理文件:

MyFile file = new KNFBFile(string.Format("{0}\\{1}.knfb", ApplicationData.Current.LocalFolder.Path, string.Format("Batch-{0:yyyyMMdd-hh-mm-ss-tt}", DateTime.Now))); 
uint iCount = 0; 
foreach (StorageFile filebatch in listFiles) 
{ 
    await App.converter.ConvertBatchJPG(filebatch); 
    IRandomAccessStream imagestream = await StreamHelpers.IBufferToStream(App.ocr.LastImageBuffer); 
    file.SavePage(iCount++, imagestream); 
} 

这种方法现在被称为:

public async Task ConvertBatchJPG(StorageFile fileSource) 
{ 
     IRandomAccessStream JPGStream = await fileSource.OpenReadAsync(); 

- >上述(OpenReadAsync)方法会导致异常 ... }

+0

你没有配置流 - 尽量把它们放到'使用(IRandomAccessStream ...){/ /这里的流代码完成},这应该确保它们被丢弃。 – Romasz

+0

我已经试过了,没有帮助,我会给它另一个尝试,虽然 – PrisonMike

+0

此外,有时它完美的作品,其他时间它不 – PrisonMike

回答

1

您需要处置filestreamoutputstream您正在检索:

StorageFile fileTarget = await folderBatch.CreateFileAsync(string.Format("batch{0}.jpg", iNumberOfFiles)); 
using(IRandomAccessStream filestream = await fileTarget.OpenAsync(FileAccessMode.ReadWrite)) 
{ 
    using(var outStream = filestream.GetOutputStreamAt(0)) 
    { 
     await outStream.WriteAsync(ImageBuffer); 
    } 
    await filestream.FlushAsync(); 
} 

既然你不改变由OpenAsync()返回的流,也可以避免创建一个中介输出流:

using(IRandomAccessStream filestream = await fileTarget.OpenAsync(FileAccessMode.ReadWrite)) 
{ 
    await filestream .WriteAsync(ImageBuffer); 
    await filestream.FlushAsync(); 
} 
+0

忘了这一点,非常感谢 – PrisonMike

相关问题