eitan barazani
eitan barazani

Reputation: 1123

Why enumerating through the Camera Roll returns wrong file name?

I took a picture with a Lumia Selfie application on 1520. The picture was stored in the "Camera Roll".

Then I selected the picture using the PhotoChooserTask. The filename of the selected picture is: C:\Data\Users\Public\Pictures\Camera Roll\WP_20150211_13_52_40_Selfie.jpg

In my code I saved the album and the file name, i.e.: FileName = "Camera Roll\WP_20150211_13_52_40_Selfie.jpg"

Later, I tried to retrieve the picture from the Camera Roll. I use the following code to enumerate through the Camera Roll album:

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

In the Output windows I see that the p.Name is now coming back as: "Taken with Lumia Selfie" rather than "WP_20150211_13_52_40_Selfie.jpg" as expected.

Can anyone provide an explanation why is that, and/or what am I doing wrong?

Thanks

Upvotes: 0

Views: 75

Answers (1)

eitan barazani
eitan barazani

Reputation: 1123

From older post on this site:

Picture.Name is not the file name. It is a metadata "title" that the creator can assign to a picture. You can use MediaLibraryExtensions.GetPath(picture) to get the file path: https://stackoverflow.com/a/17821665/2850807

BTW, for all practical purposes MediaLibrary is now a deprecated API. I suggest that you start to use the StorageFolder and StorageFile API to access files.

Upvotes: 1

Related Questions