This is a known bug with some of the MediaLibrary methods. Normally happens for big images in the size range of 4 to 16 MB.
Not sure of the bug fix status. Here is the connect link: http://connect.microsoft.com/VisualStudio/feedback/details/776453/savepicturetocameraroll-randomly-throws-nullreferrenceexception
One of the workarounds to mitigate the issue is to use the memory stream, if your code is in the UI thread and not in any worker thread:
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoStore.FileExists("fileName"))
{
using (var fileStream = isoStore.OpenFile("fileName", FileMode.Open))
{
byte[] bytes = new byte[0]; // Read bytes from fileStream
MediaLibrary library = new MediaLibrary();
library.SavePicture("name", bytes);
}
}
}
Other wprkarounds include doing a GC.Collect() before calling this method, using a try/catch to retry AND finally reduce the size of the image if possible.