2015-07-10 221 views
0

我正在嘗試構建相機應用程序。我可以拍照並保存。無法顯示拍攝的照片

但是繼承人的扭曲。如果我將照片保存在ApplicationData.Current.LocalFolder中,我可以在Image控件中顯示它。但是,當我將它保存在KnownFolders.CameraRoll中時,Image控件不會加載照片。

該照片顯示在手機的相冊(照片)中,所以不是該文件未創建。我也添加了圖片庫功能。 Theres甚至沒有例外!

async private void Capture_Photo_Click(object sender, RoutedEventArgs e) 
    { 
     //Create JPEG image Encoding format for storing image in JPEG type 
     ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); 

     //create storage file in local app storage 
     //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Photo.jpg",CreationCollisionOption.ReplaceExisting); 

     //// create storage file in Picture Library 
     StorageFile file = await KnownFolders.CameraRoll.CreateFileAsync("Photo.jpg", CreationCollisionOption.GenerateUniqueName); 


     // take photo and store it on file location. 
     await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file); 

     // Get photo as a BitmapImage using storage file path. 
     BitmapImage img = new BitmapImage(); 

     BitmapImage bmpImage = new BitmapImage(new Uri(file.Path)); 

     // show captured image on Image UIElement. 
     imagePreivew.Source = bmpImage; 
    } 

回答

1

訪問PicturLibrary文件時不同。我們需要通過調用StorageFile.OpenAsync(FileAccessMode)來加載具有所需訪問模式的文件流。

你的代碼更改爲你的工作如下:

async private void Capture_Photo_Click(object sender, RoutedEventArgs e) 
{ 
     //Create JPEG image Encoding format for storing image in JPEG type 
     ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg(); 

     //create storage file in local app storage 
     //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Photo.jpg",CreationCollisionOption.ReplaceExisting); 

     //// create storage file in Picture Library 
     StorageFile file = await KnownFolders.CameraRoll.CreateFileAsync("Photo.jpg", CreationCollisionOption.GenerateUniqueName); 


     // take photo and store it on file location. 
     await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file); 

     var picstream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); 


     BitmapImage img = new BitmapImage(); 

     img.SetSource(picstream); 

     imagePreivew.Source = img; 
} 
+0

工作!謝謝 :) –