为什么通过相机胶卷枚举会返回错误的文件名?

时间:2015-02-11 13:19:11

标签: c# windows-phone-8 camera

我在1520年用Lumia Selfie应用程序拍了一张照片。照片存储在"相机胶卷"。

然后我使用PhotoChooserTask选择了图片。所选图片的文件名是: C:\ Data \ Users \ Public \ Pictures \ Camera Roll \ WP_20150211_13_52_40_Selfie.jpg

在我的代码中我保存了专辑和文件名,即: FileName =" Camera Roll \ WP_20150211_13_52_40_Selfie.jpg"

后来,我试图从相机胶卷中检索照片。我使用以下代码来枚举相机胶卷相册:

    public BitmapImage GetPicture( string FileName )
        {
        PictureAlbum cameraRoll = null;
        PictureAlbum savedPictures = null;
        PictureAlbum samplePictures = null;
        PictureAlbum favoritePictures = null;

        int index = FileName.IndexOf("\\", StringComparison.Ordinal);
        string albumName = FileName.Remove( index, FileName.Length - index );
        string fileName = FileName.Remove( 0, index + 1 );

        foreach ( MediaSource source in MediaSource.GetAvailableMediaSources() )
            {
            if ( source.MediaSourceType == MediaSourceType.LocalDevice )
                {
                var ml = new MediaLibrary( source );
                PictureAlbumCollection allAlbums = ml.RootPictureAlbum.Albums;

                foreach ( PictureAlbum album in allAlbums )
                    {
                    if ( album.Name == "Camera Roll" )
                        {
                        cameraRoll = album;
                        }
                    else if ( album.Name == "Saved Pictures" )
                        {
                        savedPictures = album;
                        }
                    else if ( album.Name == "Sample Pictures" )
                        {
                        samplePictures = album;
                        }
                    else if ( album.Name == "Favorite Pictures" )
                        {
                        favoritePictures = album;
                        }
                    }
                }
            }

        PictureAlbum Album;
        switch ( albumName )
            {
            case "Camera Roll":
                Album = cameraRoll;
                break;

            case "Saved Pictures":
                Album = savedPictures;
                break;

            case "Sample Pictures":
                Album = samplePictures;
                break;

            case "Favorite Pictures":
                Album = favoritePictures;
                break;

            default:
                Album = null;
                break;
            }

        if ( Album == null )
            {
            return new BitmapImage();
            }

        var b = new BitmapImage();

        //foreach ( Picture p in Album.Pictures )
        foreach ( Picture p in Album.Pictures.Take( Album.Pictures.Count ) ) 
            {
            Debug.WriteLine( p.Name + "      " + fileName );
            if ( fileName != p.Name )
                {
                continue;
                }

            b.SetSource( p.GetThumbnail() );
            break;
            }

        return b;
        }

在输出窗口中,我看到p.Name现在又回来了: "采用Lumia Selfie"而不是" WP_20150211_13_52_40_Selfie.jpg"正如所料。

任何人都可以解释为什么会这样做,和/或我做错了什么?

谢谢

1 个答案:

答案 0 :(得分:1)

来自本网站上的旧帖子:

Picture.Name不是文件名。这是一个元数据" title"创作者可以分配给图片。您可以使用MediaLibraryExtensions.GetPath(图片)来获取文件路径: https://stackoverflow.com/a/17821665/2850807

BTW,出于所有实际目的,MediaLibrary现在已被弃用。我建议您开始使用StorageFolder和StorageFile API来访问文件。