In this tutorial I will show you how you could use Windows Phone 8.1’s FileOpenPicker API to access the picture gallery. It is quite simple to implement but I couldn’t find good resources which showed how it can be done. This tutorial will let you to either click or choose an image from the picture gallery and display it in your app. Here are the steps,
Step 1: Add Picture Library Capability in your Windows Phone 8.1 app
Open the Package.appmanifest file in your project and tick Picture Library under the Capabilities tab.
Step 2: Add File Open Picker as a declaration
Navigate to the Declarations tab and add File Open Picker as a supported declaration. Also under the Properties section of the declaration choose all the supported file types for your application. In our case we have added .jpg, .png, .bmp and .jpeg. You may tick the Supports any file type checkbox if you wish.
Step 3: Add a button and image to MainPage.xaml
For the purpose of demonstration, we have added an image and button control in our MainPage.xaml.
Step 4: Add global variables view and ImagePath
The variable view is of type CoreApplicationView and holds the current application view. ImagePath is a string which holds the path of the image.
Also initialize the view in the page’s default constructor.
Step 5: Add the code to call the File Open Picker on Button Click event
We call the File Open Picker API on Button click event and add the subset of supported file types to the call.
Step 6: On View activated event set the image to the MainPage
The picked file is saved to storageFile and it is then decoded to a Bitmap image before displaying it in the UI.