Windows Phone:如何在应用程序实例之间从媒体库检索相同的照片

发布于 2024-11-02 19:02:26 字数 1497 浏览 0 评论 0原文

如何在应用程序实例之间从媒体库检索相同的照片?我启动照片库,供用户通过以下方式选择照片:

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

然后在事件处理程序中,我检索所选文件的文件名,如下所示:

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

我将此信息保留在独立存储中,以便当用户再次启动应用程序时可以检索路径并像这样显示图像:

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

但是,这似乎不起作用,因为照片选择器中的照片路径是某种形式的 guid:“\Applications\Data\02E58193-119F-42E2-AD85 -C24247BE2AB0\Data\PlatformData\PhotoChooser-4edd185d-d934-4dac-8a34-758cac09d338.jpg"

每当我切换出应用程序或在页面之间移动时,Application.GetResourceStream(uri) 为 null。有更好的方法吗?

如何每次检索相同的路径,以便当我删除或终止应用程序时,我可以撤销文件并显示它?或者是否有不同的/更有效的方法来做到这一点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫弦 2024-11-09 19:02:26

我在文档中找到了答案: http:// /msdn.microsoft.com/en-us/library/gg680264%28v=pandp.11%29.aspx
基本上,照片选择器中存在一个返回临时路径的错误。微软的建议是,如果你想在应用程序实例之间使用图片,请将图片复制到独立存储中。

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.

£噩梦荏苒 2024-11-09 19:02:26

Application.GetResourceStream 将为该路径返回 null,因为 GetResourceStream 方法正在应用程序本身查找资源,而不是从设备查找资源。

要从逻辑删除重新加载简历上的相同图像,只需保留 OriginalFileName 属性,然后使用该属性创建 BitmapImage,如下所示:

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

注意: OriginalFileName 属性已经是一个字符串,因此您无需对其调用 .ToString()

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文