문제

I'm building an app that should- among other things, let the user capture a picture and then save the picture and other info (like location of where this picture was taken -using GPS of the phone and etc...) on a DataBase.

Im using a string to save the pictures to the DataBase. So far so good. My problem is that after the user has captured a picture I can not find the path of the picture anyWhere (in order to display it later to the user ) I know I can display the picture if I use a image and not a string but then I am not able to save it to the DB.

Also I used the picture string (which should be the path of the picture ) to be the primaryKey column in my table and if the string is null this will be a problem for sure.

After checking on the internet I found out that you cannot use the GeoCoordinateWatcher (for GPS) on the emulator so I had to use a random place. This led me into thinking that a picture taken by the emulator may not have a path??

Part of my code: (the Event of the camera and the bottun that saves everyting to DB)

   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
    }
}

my class:

  [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- I would like to know if I can get the path in some way... And also- if I cant get the path- does Windows have a "Better" emulator? this emulator cant do much and this is quite annoying giving the fact I dont have a WP to check my apps on.. Thanks!

도움이 되었습니까?

해결책

You already get the stream of the taken image in e.ChosenPhoto. You just need to save that.

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);
  }
}

edit:

Regarding emulator, there is nothing wrong or limited about it.

The taken image is stored in a temp file that may vanish later on that's why you need to save it locally in your isolated storage if you want to display that image again.

Regarding GPS, you can use the additional tools (just click on the '>>' button on the right side of the emulator to set various settings that you find on an actual device such as accelerometer, location, network, etc.. For GeoWatcher you can define a set of points on the map that will be played back as if the device's actual GPS location was changing.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top