【问题标题】:Get Photo file Uri using PhotoChooserTask wp8使用 PhotoChooserTask wp8 获取照片文件 Uri
【发布时间】:2014-09-19 14:41:21
【问题描述】:

对于我当前的项目,我有一个 IEnumerable 属性 ImageCollection,如下所示,

    public static IEnumerable<Uri> ImageCollection { get; set; }

其中包含可供用户选择的所有应用内照片的集合。该功能包括用户可以从图库中进行选择。我正在使用 PhotoChooserTask 和下面的代码来使用下面的代码设置图像源,

    private void photoChooserTask_Completed(object sender, PhotoResult e)
    {
        try
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);

                LibrImage.Source = image;

                ViewModelLocator.SelectedImage = image;
            }
        }
        catch (Exception)
        {
            Common.ShowMessageBox("Error occured while saving pic.");
        }
    }

但是,我想将引用 Uri 或 e.OriginalFileName 存储在数据库中。我了解到,即使将副本存储在独立存储中,我也无法实现我想要的 - 获取对使用 photoChooserTask 选择的文件的引用。我可能错了。

即使我最初的想法是将库文件的 Uri 添加到我的 ImageCollection 并将索引存储在每个项目的数据库中,任何建议或即兴创作都将受到赞赏。

注意:添加CameraCaptureTask标签,因为PhotoChooserTask标签不可用,基本逻辑相同。

编辑用于存储和检索独立存储的代码

我将 GetImageFromIsolatedStorage() 方法的结果绑定到我的图像控制源。这不起作用..我做错了什么...

public static string SaveImageToIsolatedStorage(BitmapImage bitImg)
        {
            string fname = null;

            if (bitImg != null)
            {
                fname = GetImageName();

                WriteableBitmap wbmp = new WriteableBitmap(bitImg);
                IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
                using (isf)
                {
                    if (isf.FileExists(fname)) { isf.DeleteFile(fname); }

                    using (var stream = isf.OpenFile(fname, System.IO.FileMode.OpenOrCreate))
                    {
                        wbmp.SaveJpeg(stream, bitImg.PixelWidth, bitImg.PixelHeight, 0, 100);
                        stream.Close();
                    }
                }
            }

            return fname;
        }

        private static string GetImageName()
        {
            return string.Format(
                "{0}/GalleryImage{1}.jpg",
                FileDir,
                ImageCount++);
        }

        public static BitmapImage GetImageFromIsolatedStorage(string fname)
        {
            BitmapImage img = new BitmapImage();
            try
            {
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream fileStream = isf.OpenFile(fname, FileMode.Open, FileAccess.Read))
                    {
                        img.SetSource(fileStream);
                        fileStream.Close();
                    }
                }
            }
            catch { }

            return img;
        }

【问题讨论】:

    标签: windows-phone-8 isolatedstorage cameracapturetask


    【解决方案1】:

    这是不可能的。在 Windows Phone 中,ChooserTask 和对设备库的完全访问权是两个不同的东西。

    您可以指定您的应用具有对库的完全访问权限并以这种方式处理它,或者使用ChooserTask,让用户选择照片(也可以裁剪它),然后将此图像保存到应用的隔离存储中。

    【讨论】:

    • 即使我将图像存储到手机隔离存储中,我也无法通过 Uri 引用它,是吗?
    • 我指的是 THIS 线程按照您的建议进行。
    • 如果您将图像保存到独立存储,那么是的,您可以通过 Uri 引用 ib。因为您的应用可以完全访问隔离存储。
    • 你能用一些工作样本指导/推荐我吗,我可以保存它,但无法使用 Uri 读回它。已经更新了我正在尝试的代码...
    • 您可以简单地使用以下方法获取图像,而不是调用方法:new Uri("isostore:/[yourFolder]/GalleryImage2.png", UriKind.Absolute);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2014-03-04
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    相关资源
    最近更新 更多