Question

I have the requirement to save the 5 most recent pictures from the 'Saved Pictures' album from MediaLibrary into IsolatedStorage. I am not quite sure the best way to accomplish this task. So far I am searching through the MediaLibrary for the 'Saved Pictures' album. If the album exists and pictures exist within that album, I need to take the most recent 5 images that start with the filename "TestApp" and save them to IsolatedStorage. The names will be used to update a tile, so the filepath of each image is very specific. What I have so far is as follows, I'm jus tnot sure how to save p.GetImage() (which returns an image from the MediaLibrary of type System.IO.Stream) to IsolatedStorage with the updated filename

private PictureCollection _pictures = null;

    public void StoreCycleTileImages()
    {
        string _photoPath = @"\Shared\ShellContent";
        string _photoFilename = null;
        int i = 0;

        using (MediaLibrary library = new MediaLibrary())
        {
            foreach (PictureAlbum album in library.RootPictureAlbum.Albums)
            {
                if (album.Name == "Saved Pictures")
                {
                    _pictures = album.Pictures;

                    if(_pictures != null)
                    {
                        //search for the most recent pictures in the album with the correct file name
                        foreach (var p in _pictures.Reverse())
                        {
                            if (i >= 5)
                                return;

                            if (p.Name.Substring(0,7) == "TestApp")
                            {
                                i += 1;

                                using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
                                {
                                    if (!storage.DirectoryExists(_photoPath))
                                    {
                                        storage.CreateDirectory(_photoPath);
                                    }

                                    //Update file name
                                    _photoFilename = @"" + i.ToString();

                                    if (storage.FileExists(_photoPath + @"\" + _photoFilename))
                                    {
                                        storage.DeleteFile(_photoPath + @"\" + _photoFilename);
                                    }

                                    //use p.GetImage() stream to save to IsolatedStorage with updated file name
                                    //??
                                }
                            }
                        }                            
                    }

                    break;
                }
            }
        }
    }
Was it helpful?

Solution

I've run your code - you miss the file extension while creating file name (you should consider that it can be jpg, png, or other image). My working code (copying fragment) looks like this:

photoFilename = @"" + i.ToString() + p.Name.Substring(p.Name.LastIndexOf('.'));
if (storage.FileExists(_photoPath + @"\" + _photoFilename))
{
    storage.DeleteFile(_photoPath + @"\" + _photoFilename);
}
using (IsolatedStorageFileStream file = storage.CreateFile(_photoPath + @"\" + _photoFilename))
    p.GetImage().CopyTo(file);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top