2016-08-25 43 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文件時,你有圖片庫,你可以在清單中指定它,一切都會正常工作。

否則,您可以通過文件選取器訪問存儲令牌以便稍後調用它。