2016-08-25 46 views
0

我试图从存储文件夹添加图像源。首先,我拍摄照片并将其保存到存储文件夹从StorageFile UWP添加图像源

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

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

回答

0

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

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