2

How do I retrieve the same photo from the media library between application instances? I launch the photo library for the user to select a photo via:

                        PhotoChooserTask myPhotoChooser = new PhotoChooserTask();
                        myPhotoChooser.ShowCamera = true;
                        myPhotoChooser.Show();
                        myPhotoChooser.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);

and then in the Event handler, I retrieve the file name of the selected file like this:

    private void cameraCaptureTask_Completed(object sender, PhotoResult e)
    {
        if (e.TaskResult == TaskResult.OK)
        {
                         string imagePath = e.OriginalFileName.ToString();
         }
    }

I persist this information in isolated storage so that when a user launches the application again I can retrieve the path and display the image like this:

 private  BitmapImage ConvertUriToBitmap(string pathToImage)
    {
        StreamResourceInfo streamResInfo = null;
        Uri uri = new Uri(pathToImage, UriKind.Relative);

        streamResInfo = Application.GetResourceStream(uri); //This fails! StreamResInfo is null
        BitmapImage convertedBitmap = new BitmapImage();
        convertedBitmap.SetSource(streamResInfo.Stream);
        return convertedBitmap;
    } 

However, this doesn't seem to work as the photo path from the photo chooser is some sort of guid in the form: "\Applications\Data\02E58193-119F-42E2-AD85-C24247BE2AB0\Data\PlatformData\PhotoChooser-4edd185d-d934-4dac-8a34-758cac09d338.jpg"

Application.GetResourceStream(uri) is null whenenever I switch out of the application or move between pages. Is there a better way to do this?

How do I retrieve the same path everytime so that when I tombstone or kill the app, i can retireve the file and display it? Or is there a different /more efficient way of doing it.

Kara
  • 6,115
  • 16
  • 50
  • 57
KwackMaster
  • 180
  • 2
  • 13

2 Answers2

2

I found the answer in the documentation: http://msdn.microsoft.com/en-us/library/gg680264%28v=pandp.11%29.aspx Basically, there is a bug in the photo chooser which returns a temporary path. Microsofts recommendation is to copy the picture to isolated storage if you want to use it between app instances.

KwackMaster
  • 180
  • 2
  • 13
0

Application.GetResourceStream will return null for that path because the GetResourceStream method is looking for a resource within the application itself, not from the device.

To re-load the same image on resume from tombstoning simply persist the OriginalFileName property, and then use that to create a BitmapImage as follows:

string path = /* Load the saved OriginalFileName */;
var bitmap = new BitmapImage(new Uri(path, UriKind.Absolute));
myImageControl.Source = bitmap;

NOTE: The OriginalFileName property is already a string, so you don't need to call .ToString() on it.

Derek Lakin
  • 16,179
  • 36
  • 51
  • Actually I'm finding that this doesn't completely fix the problem. The problem is that the originalFileName returned from the photochooser has a guid which is only valid for the current instance of the app. if i close and relaunch the app, that path won't work anymore (if i select the same photo again, i get a different path). What I need the absolute path to the photo which i can use between application instances. e.g. \PhotoChooser\Photo569_05_11_11.jpg. I wonder if there's a way to do this without writing my own photo chooser. – KwackMaster May 16 '11 at 21:45