简体   繁体   English

将图片保存到媒体库时,WP8 NullReferenceException

[英]WP8 NullReferenceException while saving picture to media library

Sometimes(i haven't found pattern yet) i get NullReferenceException while i am trying to save picture to media library. 有时(我还没有找到模式)我在尝试将图片保存到媒体库时收到NullReferenceException。 Problem is in method SavePicture i simply use this. 问题是方法SavePicture我只是使用它。

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. 正如您可以从代码IsolatedStorageFileStream中看到的一样,fileStream是有效的,并且不为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. 从流中的Position属性,我可以看到它不是0,所以我假设流的同一部分已经保存,但是在缓冲更多错误时发生了。 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. 它总是发生在大图像(+ 4MB)上,但不一定在每次抛出异常时都出现在同一张图片上,而我使用的是同一张图片集。 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. 如果我发现异常,并且尝试再次保存图片而不用相同的流再次打开文件(我只需要将Position设置为0),则保存图片不会出现任何问题。

Any ideas? 有任何想法吗? Any help would be appreciated. 任何帮助,将不胜感激。

This is a known bug with some of the MediaLibrary methods. 这是某些MediaLibrary方法的已知错误。 Normally happens for big images in the size range of 4 to 16 MB. 通常,大图像的大小在4到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 这是连接链接: 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: 如果您的代码在UI线程中而不在任何工作线程中,则缓解此问题的一种解决方法是使用内存流:

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. 其他wprkaround包括在调用此方法之前执行GC.Collect(),使用try / catch重试并最终减小图像的大小(如果可能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM