{"id":6125,"date":"2014-04-13T04:32:55","date_gmt":"2014-04-13T04:32:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/windows-phone-isolatedstorage-collection-of-common-programming-errors-2\/"},"modified":"2014-04-13T04:32:55","modified_gmt":"2014-04-13T04:32:55","slug":"windows-phone-isolatedstorage-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/13\/windows-phone-isolatedstorage-collection-of-common-programming-errors-2\/","title":{"rendered":"Windows Phone IsolatedStorage-Collection of common programming errors"},"content":{"rendered":"<p>I have a application that needs to download and save files on a device &#8211; videos.<\/p>\n<p>Videos are short ~ 10min and in poor quality, which means that their size is minimal.<\/p>\n<p>So, the problem is that when i download some files &#8211; all goes nice, but some files fail with error: Out of memory exception. Logically i think that files less than some size ( for example 50MB ) download nicely, but higher &#8211; exception.<\/p>\n<p>Here is my code:<\/p>\n<pre><code>private void btnDownload2_Click(object sender, RoutedEventArgs e)\n    {\n        WebClient webClient = new WebClient();\n        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);\n        webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);\n        webClient.OpenReadAsync(new Uri(\"http:\/\/somelink\/video\/nameOfFile.mp4\"));\n    }\n\nvoid webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)\n    {\n        try\n        {\n            if (progressMedia.Value  maxAvailableSpace)\n        {\n            if (!isolatedStorageFile.IncreaseQuotaTo(isolatedStorageFile.Quota + quotaSizeDemand))\n            {\n                CanSizeIncrease = false;\n                return CanSizeIncrease;\n            }\n            CanSizeIncrease = true;\n            return CanSizeIncrease;\n        }\n        return CanSizeIncrease;\n    }\n\n    void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)\n    {\n        try\n        {\n            if (e.Result != null)\n            {\n\n                #region Isolated Storage Copy Code\n                isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();\n\n                bool checkQuotaIncrease = IncreaseIsolatedStorageSpace(e.Result.Length);\n\n                string VideoFile = \"PlayFile.wmv\";\n                isolatedStorageFileStream = new IsolatedStorageFileStream(VideoFile, FileMode.Create, isolatedStorageFile);\n                long VideoFileLength = (long)e.Result.Length;\n                byte[] byteImage = new byte[VideoFileLength];\n                e.Result.Read(byteImage, 0, byteImage.Length);\n                isolatedStorageFileStream.Write(byteImage, 0, byteImage.Length);\n\n                #endregion\n\n                mediaFile.SetSource(isolatedStorageFileStream);\n                mediaFile.Play();\n                progressMedia.Visibility = Visibility.Collapsed;\n\n\n            }\n        }\n        catch (Exception ex)\n        {\n            MessageBox.Show(ex.Message);\n        }\n    }\n\n    private void mediaFile_MediaEnded(object sender, RoutedEventArgs e)\n    {\n        MessageBoxResult res = MessageBox.Show(\"Do you want to Replay the file\", \"Decide\", MessageBoxButton.OKCancel);\n\n        if (res == MessageBoxResult.OK)\n        {\n            mediaFile.Play();\n        }\n        else\n        {\n            isolatedStorageFileStream.Close();\n            isolatedStorageFile.Dispose();\n            mediaFile.ClearValue(MediaElement.SourceProperty);\n        }\n    }\n<\/code><\/pre>\n<p>Exception details:<\/p>\n<blockquote>\n<p>System.OutOfMemoryException was unhandled Message: An unhandled exception of type &#8216;System.OutOfMemoryException&#8217; occurred in System.Windows.ni.dll<\/p>\n<\/blockquote>\n<p>Exception image:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/R2NjU.png\" \/><\/p>\n<p>Is there a workaround for this?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a application that needs to download and save files on a device &#8211; videos. Videos are short ~ 10min and in poor quality, which means that their size is minimal. So, the problem is that when i download some files &#8211; all goes nice, but some files fail with error: Out of memory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6125","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6125"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6125\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}