Question

Sometimes(i haven't found pattern yet) i get NullReferenceException while i am trying to save picture to media library. Problem is in method SavePicture i simply use this.

using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    if (isoStore.FileExists("fileName"))
    {
        using (var fileStream = isoStore.OpenFile("fileName", FileMode.Open))
        {
            MediaLibrary library = new MediaLibrary();
            library.SavePicture("name", fileStream);
        }
    }
}

fileStream is as you can see from code IsolatedStorageFileStream and is valid and not null. This is my stack trace

at Microsoft.Xna.Framework.Media.UnsafeNativeMethods.MediaLibrary_SavePicture(String name, Int32 nameLength, UInt32 stream, UInt32& picture)
at Microsoft.Xna.Framework.Media.MediaLibrary.SavePicture(String name, Stream source)

From Position property in stream i can see that its not 0 so i am assuming that same part of stream was already saved but while buffering for more something wrong happened. it always happen on big images(+4MB) but not necessarily on same picture every time exception is thrown and i am using same collection of pictures. If i catch the exception and i try to save picture again without opening file again just with same stream(i only have to set Position to 0) then picture is saved without any problems.

Any ideas? Any help would be appreciated.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top