2014-09-19 22 views
0

照片文件URI對於我目前的項目,我有一個IEnumerable財產ImageCollection如下,獲取使用PhotoChooserTask WP8

public static IEnumerable<Uri> ImageCollection { get; set; } 

持有的所有可用供用戶選擇應用程序內照片集。該功能包括用戶可以從圖庫中進行選擇。我使用PhotoChooserTask和下面的代碼來設置使用下面的代碼的圖像源,

private void photoChooserTask_Completed(object sender, PhotoResult e) 
    { 
     try 
     { 
      if (e.TaskResult == TaskResult.OK) 
      { 
       BitmapImage image = new BitmapImage(); 
       image.SetSource(e.ChosenPhoto); 

       LibrImage.Source = image; 

       ViewModelLocator.SelectedImage = image; 
      } 
     } 
     catch (Exception) 
     { 
      Common.ShowMessageBox("Error occured while saving pic."); 
     } 
    } 

不過,我想存儲參考,URI或e.OriginalFileName,在數據庫中。我瞭解到,即使將商店複製到獨立存儲中,我也無法實現我正在尋找的功能 - 獲取對使用photoChooserTask選擇的文件的引用。我可能是錯的。

即使我最初的想法是將圖庫文件的Uri添加到我的ImageCollection中,並將索引存儲在數據庫中的每個項目,任何建議或即興感謝讚賞。

注意:添加CameraCaptureTask標記爲PhotoChooserTask標記不可用,基本邏輯相同。

EDIT代碼用於存儲和檢索來自IsolatedStorage

我綁定GetImageFromIsolatedStorage的(結果)方法來我圖像控制源。這不工作..我做錯了什麼...

public static string SaveImageToIsolatedStorage(BitmapImage bitImg) 
     { 
      string fname = null; 

      if (bitImg != null) 
      { 
       fname = GetImageName(); 

       WriteableBitmap wbmp = new WriteableBitmap(bitImg); 
       IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication(); 
       using (isf) 
       { 
        if (isf.FileExists(fname)) { isf.DeleteFile(fname); } 

        using (var stream = isf.OpenFile(fname, System.IO.FileMode.OpenOrCreate)) 
        { 
         wbmp.SaveJpeg(stream, bitImg.PixelWidth, bitImg.PixelHeight, 0, 100); 
         stream.Close(); 
        } 
       } 
      } 

      return fname; 
     } 

     private static string GetImageName() 
     { 
      return string.Format(
       "{0}/GalleryImage{1}.jpg", 
       FileDir, 
       ImageCount++); 
     } 

     public static BitmapImage GetImageFromIsolatedStorage(string fname) 
     { 
      BitmapImage img = new BitmapImage(); 
      try 
      { 
       using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        using (IsolatedStorageFileStream fileStream = isf.OpenFile(fname, FileMode.Open, FileAccess.Read)) 
        { 
         img.SetSource(fileStream); 
         fileStream.Close(); 
        } 
       } 
      } 
      catch { } 

      return img; 
     } 

回答

0

這是不可能的。 Windows Phone中的ChooserTask和對設備庫的完全訪問是兩個不同的事情。

要麼指定您的應用程序已完全訪問庫並以此方式處理它,要麼使用ChooserTask,讓用戶選擇照片(還可以裁剪它),然後將此圖像保存到應用程序的隔離存儲中。

+0

即使我將圖像存儲到手機隔離存儲,我將無法通過Uri引用它,是嗎? – 2014-09-21 12:59:14

+0

我指的是[THIS](http://stackoverflow.com/questions/22375669/photochoosertask-save-and-view-image-in-windows-phone-8?rq=1)線程按照你的建議進行。 – 2014-09-21 13:45:24

+0

如果將圖像保存到獨立存儲中,那麼是的,您可以參考Uri的ib。因爲您的應用可以完全訪問隔離存儲。 – Filip 2014-09-21 17:10:32

相關問題