Over a million developers have joined DZone.

Windows Phone Apps - Saved Photos to the User and Back

DZone's Guide to

Windows Phone Apps - Saved Photos to the User and Back

· Mobile Zone ·
Free Resource

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;

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)

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)

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.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}