storageFile.OpenReadAsync上的UnauthorizedAccessException

时间:2016-11-18 10:11:08

标签: c# stream uwp buffer storage

用户可以拍摄多张照片。这些图片存储在ApplicationData.Current.LocalFolder:

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();

- &gt;上面的方法(OpenReadAsync)导致异常      ...      }

1 个答案:

答案 0 :(得分:1)

您需要处理filestream 您正在检索的outputstream

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();
 }