2013-05-13 90 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

相关问题