title: Download mp3 and save as Ringtone
body: <p>I want to download an mp3 File from a Link on the Phone(Isolated Storage) and then save it as a Ringtone.</p>
  
  <p>But my Code doesn't work correctly... It gives me back an Error:</p>
  
  <pre><code>  System.InvalidOperationException: Path must point to a file in your Isolated Storage or Application Data directory.
  </code></pre>
  
  <p>I call the function like this:</p>
  
  <pre><code>   private void getRingtone_Click(object sender, EventArgs e)
      {
          ringtone = new Ringtone();
          ringtone.DownloadFile(GlobalVariables.Url, GlobalVariables.filename);
          //ringtone.SaveRingtone();
  
      }
  </code></pre>
  
  <p>The Globalvariables Url is like: www.example.com/mp3/M/myfile.dmf.mp3 (if you need for testing i can give you my real url)</p>
  
  <p>and the filename is like: myfile.dmf.mp3</p>
  
  <p>This is in the Ringtone Class:</p>
  
  <pre><code>   WebClient _webClient; // Used for downloading mp3
      private bool _playSoundAfterDownload;
      MediaElement mediaSound;
      SaveRingtoneTask saveRingtoneChooser;
  
  
      public void DownloadFile(string uri, string filename)
      {
          _webClient = new WebClient();
          saveRingtoneChooser = new SaveRingtoneTask();
          saveRingtoneChooser.Completed += new EventHandler&lt;TaskEventArgs&gt;(saveRingtoneChooser_Completed);
          _webClient.OpenReadCompleted += (s1, e1) =&gt;
          {
              if (e1.Error == null)
              {
                  try
                  {
                      string fileName = GlobalVariables.filename;
                      bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
  
                      if (isSpaceAvailable)
                      {
                          // Save mp3 to Isolated Storage
                          using (var isfs = new IsolatedStorageFileStream(fileName,
                                              FileMode.CreateNew,
                                              IsolatedStorageFile.GetUserStoreForApplication()))
                          {
                              long fileLen = e1.Result.Length;
                              byte[] b = new byte[fileLen];
                              e1.Result.Read(b, 0, b.Length);
                              isfs.Write(b, 0, b.Length);
                              isfs.Flush();
                          }
  
                          if (_playSoundAfterDownload)
                          {
                              _playSoundAfterDownload = false;
                              SaveRingtone();
                          }
                      }
                      else
                      {
                          MessageBox.Show("Not enough to save space available to download mp3.");
                      }
  
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                  }
              }
              else
              {
                  MessageBox.Show(e1.Error.Message);
              }
          };
          SaveRingtone();
      }
  
      // Check to make sure there are enough space available on the phone
      // in order to save the image that we are downloading on to the phone
      private bool IsSpaceIsAvailable(long spaceReq)
      {
          using (var store = IsolatedStorageFile.GetUserStoreForApplication())
          {
  
              long spaceAvail = store.AvailableFreeSpace;
              if (spaceReq &gt; spaceAvail)
              {
                  return false;
              }
              return true;
          }
      }
  </code></pre>
  
  <p>i did it like this example: <a href="http://blog.toetapz.com/2010/11/29/how-to-download-and-save-mp3-to-isolatedstorage/" rel="nofollow">http://blog.toetapz.com/2010/11/29/how-to-download-and-save-mp3-to-isolatedstorage/</a></p>
  
  <p>the rest is the safe ringtone part. This works when i add the mp3, directly to my projekt and use the appdata:xyz.mp3 part of the code.</p>
  
  <pre><code>     private void SaveRingtone()
      {
          try
          {
              //saveRingtoneChooser.Source = new Uri("appdata:/myTone.wma");
              saveRingtoneChooser.Source = new Uri("isostore:/"+GlobalVariables.filename); 
  
              saveRingtoneChooser.DisplayName = "My custom ringtone";
  
              saveRingtoneChooser.Show();
          }
          catch (System.InvalidOperationException ex)
          {
              MessageBox.Show("An error occurred."); //Error appears here.
          }
      }
      void saveRingtoneChooser_Completed(object sender, TaskEventArgs e)
      {
          switch (e.TaskResult)
          {
              //Logic for when the ringtone was saved successfully
              case TaskResult.OK:
                  MessageBox.Show("Ringtone saved.");
                  break;
  
              //Logic for when the task was cancelled by the user
              case TaskResult.Cancel:
                  MessageBox.Show("Save cancelled.");
                  break;
  
              //Logic for when the ringtone could not be saved
              case TaskResult.None:
                  MessageBox.Show("Ringtone could not be saved.");
                  break;
          }
      }
  }
  </code></pre>
  
  <p>I hope my Problem is understandable. Thanks.</p>