首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnauthorizedAccessException at storageFile.OpenReadAsync

UnauthorizedAccessException at storageFile.OpenReadAsync
EN

Stack Overflow用户
提问于 2016-11-18 10:11:07
回答 1查看 136关注 0票数 0

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

代码语言:javascript
复制
folderBatch = await ApplicationData.Current.LocalFolder.CreateFolderAsync("Batch", CreationCollisionOption.OpenIfExists);

当用户拍照时,会触发此方法:

代码语言:javascript
复制
    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调用:

代码语言:javascript
复制
    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;
    }

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

代码语言:javascript
复制
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);
}

这个方法现在被称为:

代码语言:javascript
复制
 public async Task ConvertBatchJPG(StorageFile fileSource)
 {
      IRandomAccessStream JPGStream = await fileSource.OpenReadAsync();

->上面的方法(OpenReadAsync)导致异常.}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-18 10:28:48

您需要释放正在检索的filestream outputstream

代码语言:javascript
复制
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()返回的流,所以也可以避免创建中间输出流:

代码语言:javascript
复制
using(IRandomAccessStream filestream = await fileTarget.OpenAsync(FileAccessMode.ReadWrite))
{
    await filestream .WriteAsync(ImageBuffer);
    await filestream.FlushAsync();
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40674337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档