2016-11-18 42 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

相關問題