从StorageFile UWP添加图像源

时间:2016-08-25 10:47:28

标签: c# windows-10 uwp storage readfile

我正在尝试从存储文件夹添加图像源。首先,我捕获照片并将其保存到存储文件夹

        Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

        private async void btnPhoto_Click(object sender, RoutedEventArgs e)
    {
            // Create the file that we're going to save the photo to.
        var file = await storageFolder.CreateFileAsync("sample.jpg", CreationCollisionOption.GenerateUniqueName);

        // Update the file with the contents of the photograph.         

        await _mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), file);
    }

在下一页我应该从缓存中获取照片,但是当我创建sampleFile时我有一个例外

        public async void GetImage()
    {

        string filename = "sample.jpg";
        Windows.Storage.StorageFile sampleFile = 
            await Windows.Storage.KnownFolders.DocumentsLibrary.GetFileAsync(filename); // Here I have exception {"Access is denied.\r\n"} System.UnauthoriziedAccessException


        BitmapImage img = new BitmapImage();
        img = await LoadImage(sampleFile);

        image1.Source = img;
    }


    private static async Task<BitmapImage> LoadImage(StorageFile file)
    {
        BitmapImage bitmapImage = new BitmapImage();
        FileRandomAccessStream stream = (FileRandomAccessStream)await file.OpenAsync(FileAccessMode.Read);

        bitmapImage.SetSource(stream);

        return bitmapImage;
    }

我做错了什么?我怎样才能访问存储文件夹?

1 个答案:

答案 0 :(得分:0)

除非用户允许,否则您无法访问文档库。您可以在清单中指定文档库访问权限,但仅限于某些文件,并且此类应用程序可能永远不会在Windows应用商店中发布,除非它真的需要Microsoft所述的访问权限。当你使用.jpg文件时,你有图片库,你可以在清单中指定它,一切都会正常工作。

否则,您可以通过文件选择器进行访问,存储令牌以便以后调用它。