2013-05-13 112 views
0

我正在構建一個應用程序,該應用程序應該 - 除其他外,讓用戶捕捉圖片,然後保存圖片和其他信息(如拍攝該圖片的位置 - 使用手機的GPS等...)在數據庫上。Windows Phone 7.1 - 保存相機拍攝的照片路徑CapsureTask

使用字符串將圖片保存到數據庫。到現在爲止還挺好。我的問題是,在用戶捕獲圖片後,我無法在任何地方找到圖片的路徑(爲了稍後向用戶顯示) 我知道如果使用圖片而不是字符串,我可以顯示圖片但後來我無法將它保存到數據庫。

此外,我用圖片字符串(應該是圖片的路徑)作爲我的表中的primaryKey列,如果字符串爲null,這將是一個肯定的問題。

在互聯網上檢查後,我發現你不能在模擬器上使用GeoCoordinateWatcher(用於GPS),所以我必須使用隨機的地方。 這使我認爲模擬器拍攝的照片可能沒有路徑?我的代碼

部分:

void c_Completed(object sender, PhotoResult e) 
    { 
     if (e.TaskResult == TaskResult.OK) 
     { 
      MessageBox.Show(e.ChosenPhoto.Length.ToString()); 


      //Code to display the photo on the page in an image control named myImage. 
      BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); 
      bmp.SetSource(e.ChosenPhoto); 
      myImage.Source = bmp;//display the picture right after taking it. before saving to DB 
      p.UrlImage = bmp.UriSource.AbsolutePath;//Do not Work! 

     } 
    } 

    private void saveToDB_Click(object sender, RoutedEventArgs e) 
    { 

     p.Description = DesriptionList.SelectedValue.ToString();//description of the pic 
     p.Date = DateTime.Today;//date picture taken 

     GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher(); 
     var myPosition = myWatcher.Position; 
     p.Location = myPosition.Location.Altitude+" "+myPosition.Location.Latitude;//do not work with emulator 
     p.Location = "Some Where Over The Rainbow"; 
     MainPage.m._bl.addPic(p);//add pic to DB 
     MessageBox.Show("Added Successfully! :)"); 
     NavigationService.Navigate(new Uri(@"/Intro.xaml", UriKind.Relative));//go to another page 
    } 
} 

我的課(相機和保存everyting到DB的bottun的事件):

[Table] 
public class Picture 
{ 
    public Picture() 
    { 

    } 

    public Picture(string l, string d, string url, DateTime da) 
    { 
     Location = l; 
     Description = d; 
     UrlImage = url; 
     Date = da; 
    } 
    string location; 

    [Column] 
    public string Location 
    { 
     get { return location; } 
     set { location = value; } 
    } 
    string urlImage; 

    [Column (IsPrimaryKey=true)] 
    public string UrlImage 
    { 
     get { return urlImage; } 
     set { urlImage = value; } 
    } 
    DateTime date; 
    [Column] 
    public DateTime Date 
    { 
     get { return date; } 
     set { date = value; } 
    } 
    string description; 
    [Column] 
    public string Description 
    { 
     get { return description; } 
     set { description = value; } 
    } 
} 

}

Anyway-我會想知道我能否以某種方式獲得路徑... 而且,如果我無法獲得路徑 - Windows是否具有「更好」模擬器? 這個模擬器不能做很多,這是相當惱人的事實,我沒有一個WP來檢查我的應用程序.. 謝謝!

回答

0

您已經在e.ChosenPhoto中獲取拍攝圖像的流。你只需要保存它。

var imageBytes = new byte[e.ChosenPhoto.Length]; 
e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length); 

using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { 
    using (var stream = isoFile.CreateFile("myImage.jpg")) { 
    stream.Write(imageBytes, 0, imageBytes.Length); 
    } 
} 

編輯:

關於模擬器,有什麼不對或限制它。

拍攝的圖像存儲在臨時文件中,以後可能會消失,這就是爲什麼如果要再次顯示該圖像,您需要將其保存在隔離存儲中的本地。

關於GPS,您可以使用其他工具(只需點擊模擬器右側的'>>'按鈕即可設置您在實際設備上找到的各種設置,如加速度計,位置,網絡等..對於GeoWatcher,您可以在地圖上定義一組點,以便設備的實際GPS位置發生變化。

+0

非常感謝!:) – nati 2013-05-23 18:37:16