locked
Retrieve ALL Photo Albums RRS feed

  • Question

  • I'm trying to retrieve ALL of the photo albums on the WP7.
    The "all" is capitalized because there will always be albums within albums within albums and so on. How do I retrieve each and every one of those, while filtering out albums without any pictures in them.
    Thanks for your help!
    Wednesday, January 19, 2011 5:42 AM

Answers

  • Thanks KAW 24!
    Great idea to make a method... I was thinking it was impossible to continue the loop!
    Here's an edit to make it get the first picture of each album to display it.
     
            private void PopulateViewModelsFromAlbum(PictureAlbum album)  
            {  
                //Get the first picture in each album to display it  
                if (album.Pictures.Count > 0)  
                {  
                    WriteableBitmap bmp = new WriteableBitmap(100, 100);  
                    bmp.LoadJpeg(album.Pictures[0].GetImage());  
                    myAlbums.Items.Add(new ItemViewModel()  
                    {  
                        ImageSource = bmp,  
                        LineOne = album.Name  
                    });  
                }  
                //Do the same for each child album  
                foreach (PictureAlbum childAlbum in album.Albums)  
                    PopulateViewModelsFromAlbum(childAlbum);  
            }  
     
     
            private void LoadPhotoAlbums()  
            {  
                var mediaLibrary = new MediaLibrary();  
                PopulateViewModelsFromAlbum(mediaLibrary.RootPictureAlbum);  
            } 

    Thanks for your help!
    Sunday, January 23, 2011 7:28 PM
  • Try something like the following (untested):

    var mediaLibrary = new MediaLibrary();  
    PopulateViewModelsFromAlbum(mediaLibrary.RootPictureAlbum, myAlbums.Items);  
     
    private static void PopulateViewModelsFromAlbum(PictureAlbum album, ICollection<ItemViewModel> viewModels) {  
      foreach (PictureAlbum childAlbum in album.Albums) {  
        PopulateViewModelsFromAlbum(childAlbum, viewModels);  
      }  
      foreach (Picture picture in album.Pictures) {  
        var bitmap = new WriteableBitmap(100, 100);  
        bitmap.LoadJpeg(picture.GetImage());  
        viewModels.Add(new ItemViewModel() {  
          ImageSource = bitmap,  
          LineOne = album.Name  
        });  
      }  
    }  
     
    Friday, January 21, 2011 5:46 PM

All replies

  • Unfortunately I believe it's not possibile at this time.

    Maybe it will be added in the future, but I think it will take some times as there are many other features coming on their way. This feature involves security also, 'cause since apps don't have to ask permission for sending data, personal photos would be able to leak on the net. I think if it would be ever added, it would be triggered by the capability system.

    Regards
    Wednesday, January 19, 2011 9:55 AM
  • Hi ByteStorms,
    It is possible to access the photos on a phone. And if you did send personal photos to a database, that would be caught in the certification process and stopped.

    I guess I should elaborate on my problem. Here's my code:
                MediaLibrary mediaLibrary = new MediaLibrary();  
                PictureAlbumCollection albums = mediaLibrary.RootPictureAlbum.Albums;  
                foreach (PictureAlbum album in albums)  
                {  
                    if (album.Pictures.Count > 0)  
                    {  
                        WriteableBitmap bmp = new WriteableBitmap(100, 100);  
                        bmp.LoadJpeg(album.Pictures[0].GetImage());  
                        myAlbums.Items.Add(new ItemViewModel()  
                        {  
                            ImageSource = bmp,  
                            LineOne = album.Name  
                        });  
                    }  
                    if (album.Albums.Count > 0)  
                    {  
                        foreach (PictureAlbum album2 in album.Albums)  
                        {  
                            if (album.Pictures.Count > 0)  
                            {  
                                WriteableBitmap bmp = new WriteableBitmap(100, 100);  
                                bmp.LoadJpeg(album.Pictures[0].GetImage());  
                                myAlbums.Items.Add(new ItemViewModel()  
                                {  
                                    ImageSource = bmp,  
                                    LineOne = album.Name  
                                });  
                            }  
                            //Keep going on in the loop, going into album2's albums, then album3's albums, and so on, but there is no way to code this. 
                        }  
                    }  
                } 
    I don't know how to continue the loop of going through the album's albums, and then that album's album.
    This is mainly what I am asking for help with.
    I have a main idea where I have one album in a while loop that will continue until I break from it. This album would be changed to the album that it is currently proccessing and then it process the albums in it.
    Thanks for your help!
    Thursday, January 20, 2011 3:03 AM
  • Try something like the following (untested):

    var mediaLibrary = new MediaLibrary();  
    PopulateViewModelsFromAlbum(mediaLibrary.RootPictureAlbum, myAlbums.Items);  
     
    private static void PopulateViewModelsFromAlbum(PictureAlbum album, ICollection<ItemViewModel> viewModels) {  
      foreach (PictureAlbum childAlbum in album.Albums) {  
        PopulateViewModelsFromAlbum(childAlbum, viewModels);  
      }  
      foreach (Picture picture in album.Pictures) {  
        var bitmap = new WriteableBitmap(100, 100);  
        bitmap.LoadJpeg(picture.GetImage());  
        viewModels.Add(new ItemViewModel() {  
          ImageSource = bitmap,  
          LineOne = album.Name  
        });  
      }  
    }  
     
    Friday, January 21, 2011 5:46 PM
  • Sorry, my fault! I didn't know that could be possibile.

    However I was going to suggest you to use a recursive function, that's the simplest way to achieve that, as suggested by KAV 24.
    Saturday, January 22, 2011 9:54 AM
  • Thanks KAW 24!
    Great idea to make a method... I was thinking it was impossible to continue the loop!
    Here's an edit to make it get the first picture of each album to display it.
     
            private void PopulateViewModelsFromAlbum(PictureAlbum album)  
            {  
                //Get the first picture in each album to display it  
                if (album.Pictures.Count > 0)  
                {  
                    WriteableBitmap bmp = new WriteableBitmap(100, 100);  
                    bmp.LoadJpeg(album.Pictures[0].GetImage());  
                    myAlbums.Items.Add(new ItemViewModel()  
                    {  
                        ImageSource = bmp,  
                        LineOne = album.Name  
                    });  
                }  
                //Do the same for each child album  
                foreach (PictureAlbum childAlbum in album.Albums)  
                    PopulateViewModelsFromAlbum(childAlbum);  
            }  
     
     
            private void LoadPhotoAlbums()  
            {  
                var mediaLibrary = new MediaLibrary();  
                PopulateViewModelsFromAlbum(mediaLibrary.RootPictureAlbum);  
            } 

    Thanks for your help!
    Sunday, January 23, 2011 7:28 PM
  • Hi,I have need same thing but it does not work for me.if you done successfully then tell me how to done these 

     1)List of Albums  name with the First or last Image in Each Album Displayed also total image count in particular album.Same as if user select Album then display album structure(eg.

      Album Name:-A)(I)Camera Roll --(II)display camera roll first image in grid ---(III)and total number of images stored in                                       Camera     Roll.)

                         :-B)(I)Saved Pictures --(II)display Saved Pictures first image in grid ---(III)and total number of images stored in                                       Saved Pictures)    etc way.If anyone solve these problem then send me simple code for above query.Thanks.

    Tuesday, February 25, 2014 10:28 AM