.NET Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Windows Phone Apps - Saved Photos to the User and Back

05.17.2013
| 1953 views |
  • submit to reddit

For more than one scenario, the developer might want to have access to the pictures stored in the device photo hub. That can be done in two ways - either by using PhotoChooserTask  which is the most common way to retrieve the image content, given that the user himself will select the single entity that needs to be read, or with the help of MediaLibrary - an XNA sub-layer that allows direct access to the media directory.

The minor gotcha comes when you try to retrieve the file by name. Take, for example, the Baconit app - a reddit client for Windows Phone that also allows its users to store images that are displayed through it.


Let's now say that I want to get the file that I just saved through one of my own applications, using PhotoChooserTask. Using the standard routine:

PhotoChooserTask task = new PhotoChooserTask();
task.Completed += task_Completed;
task.Show();


Once the task is completed, I am simply writing the name of the selected file in the output window:

void task_Completed(object sender, PhotoResult e)
{
    Debug.WriteLine(e.OriginalFileName);
}


Now let's say that I changed my mind, and decided to use MediaLibrary to get the data from the image stream. To begin, I am getting the list of files that I can access in the Saved Pictures album:

MediaLibrary library = new MediaLibrary();

var pictureAlbum = (from c in library.RootPictureAlbum.Albums where c.Name == "Saved Pictures" select c).FirstOrDefault();
if (pictureAlbum != null)
{
    foreach (var pic in pictureAlbum.Pictures)
    {
        Debug.WriteLine(pic.Name);
    }
}


You can see that for some files, the extension is present (for those that are represented through the full path) and for some - its missing. One should not assume that an extension in this case should be simply concatenated with the core file name - this is bound to cause problems in situations where the developer does not know that is the original file format. For example, it could be BMP, PNG, GIF or JPEG - these are, obviously, not interchangeable.

The solution to this, if you need to obtain files without an extension, would be to place those files in the isolated storage once obtained through the chooser task, and then retrieve them from there instead of relying on the XNA sub-layer for this.