0

I'm building a FTP application for Windows Phone 8, and want to save the downloaded songs from the isolated storage to the media library. I check if the file exists in isostore, and it returns true, but when I'm using the SaveSong method it always throws an exception. Here is the code sample:

  private async void contextMenuItem1_Click(object sender, RoutedEventArgs e)
    {
        string fileName = (sender as MenuItem).DataContext.ToString();
        MediaLibrary library = null;

        ......

        else if (fileName.EndsWith(".mp3") || fileName.EndsWith(".wav") || fileName.EndsWith(".aac"))
        {                
           IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

                if (myIsolatedStorage.FileExists(fileName))
                {
                    library = new MediaLibrary();
                    StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

                    if (localFile != null)
                    {
                        //MessageBox.Show("StorageFile is: " + localFile.Name);
                        try
                        {
                            library.SaveSong(new Uri(localFile.Name, UriKind.RelativeOrAbsolute), null, SaveSongOperation.CopyToLibrary);
                            //MediaLibraryExtensions.SaveSong(media, new Uri(fileName, UriKind.RelativeOrAbsolute), null, SaveSongOperation.CopyToLibrary);
                        }

                        catch (InvalidOperationException ex)
                        {
                            MessageBox.Show("Exception caught: " + ex.Message);
                        }
                    }
                }

                else
                    MessageBox.Show("File does not exist in isostore");         
        }
    }

I wolud be very grateful if anybody could help me, thx.

1 Answers1

0

If your file name or file path is null then this exception comes.Also please verify ID_CAP_MEDIALIB_AUDIO capability added or not.

FYI

vITs
  • 1,651
  • 12
  • 30
  • I have found the solution. It seems that the stream of the file was not closed, so I had these problems. Sometimes I could save the song, othertime I couldn't. After I close the file stream it seems to work without any problems – user3744413 Jul 08 '14 at 21:11
  • `IBuffer databuffer = dataReader.DetachBuffer(); RaiseFtpFileTransferProgressedEvent(databuffer.Length, false); await ftpFileInfo.LocalFileStream.WriteAsync(databuffer.ToArray(), 0, Convert.ToInt32(databuffer.Length)); } await ftpFileInfo.LocalFileStream.FlushAsync(); ftpFileInfo.LocalFileStream.Close(); dataReader.Dispose(); dataReader = null;` – user3744413 Jul 08 '14 at 21:28