2015-08-23 57 views
0

我想以編程方式設置我的手機的鈴聲。 通過一些搜索,我發現這個代碼WP8.1 SaveRingtoneTask InvalidOperationException

private void RingToneSave() 
     { 
      SaveRingtoneTask ringtoneTask = new SaveRingtoneTask(); 
      ringtoneTask.Completed += saveRingtoneChooser_Completed; 
      ringtoneTask.Source = new Uri(@"C:\Data\Programs\{9519D660-4D38-497F-9584-6497FF78C693}\Install\Craig David.wma"); 
      ringtoneTask.DisplayName = "Ringtone"; 
      ringtoneTask.Show(); 
     } 

然而ringtoneTask.Show();拋出System.InvalidOperationException 以下是完整的詳細的異常:

An exception of type 'System.InvalidOperationException' occurred in Microsoft.Phone.ni.dll but was not handled in user code 

Additional information: Path must point to a file in your Isolated Storage or Application Data directory. 

然而,路徑指向我的獨立存儲一個文件,因爲我以前曾下載並將該文件保存到我的隔離存儲中。 我還使用IsoStorySpy(一種檢查手機的隔離存儲的工具)來確保文件位於獨立存儲中。

我理解錯了嗎?有沒有另一種方法來設置我的手機的鈴聲,而不使用SaveRingtoneTask

更新

private void RingToneSave(Uri sURI) 
     { 
      SaveRingtoneTask ringtoneTask = new SaveRingtoneTask(); 
      ringtoneTask.Completed += saveRingtoneChooser_Completed; 
      ringtoneTask.Source = sURI; 
      ringtoneTask.DisplayName = "Ringtone"; 
      ringtoneTask.Show(); 
     } 

    public async Task<Problem> DownloadFileFromWeb(Uri uriToDownload, string fileName, CancellationToken cToken) 
     { 
      try 
      { 
       using (Stream mystr = await DownloadFile(uriToDownload)) 
       using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication()) 
       { 
        if (ISF.FileExists(fileName)) 
        { 
         ISF.DeleteFile(fileName); 
        } 
        using (IsolatedStorageFileStream file = ISF.CreateFile(fileName)) 
        { 
         const int BUFFER_SIZE = 8192; 
         byte[] buf = new byte[BUFFER_SIZE]; 

         int bytesread = 0; 

         while ((bytesread = await mystr.ReadAsync(buf, 0, BUFFER_SIZE)) > 0) 
         { 
          double percentage = ((double)file.Length/(double)mystr.Length) * (double)100; 
          textBlock.Text = Math.Round(percentage).ToString() + "%"; 
          cToken.ThrowIfCancellationRequested(); 
          file.Write(buf, 0, bytesread); 
         } 
         sRingTonePath = file.Name; 
        } 

       } 
       RingToneSave(new Uri(sRingTonePath)); 
       return Problem.Ok; 
      } 
      catch (Exception exc) 
      { 
       if (exc is OperationCanceledException) 
        return Problem.Cancelled; 
       else 
       { 
        MessageBox.Show(exc.Message); 
        return Problem.Other; 
       } 
      } 
     } 

回答

1

您正在使用的路徑是無效的,你應該做的就是下載你想要的音樂文件在你的獨立存儲,然後指向路徑作爲源。

而且沒有其他方法來設置鈴聲。

如果您只是需要一種方法來下載鈴聲使用app

Stream st = await new WebClient().OpenReadTaskAsync(Link); 

using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) 
      { 
       using (IsolatedStorageFileStream file = new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, isf)) 
       { 
        using (var fs = new StreamWriter(file)) 
        { 
         byte[] bytesInStream = new byte[st.Length]; 
         st.Read(bytesInStream, 0, (int)bytesInStream.Length); 
         file.Write(bytesInStream, 0, bytesInStream.Length); 
         file.Flush(); 
        } 
       } 
      } 
     } 


SaveRingtoneTask task = new SaveRingtoneTask(); 
task.Source = new Uri(string.Format("isostore:/{0}"selected.FileName),UriKind.Absolute);      
task.Completed += task_Completed; 
task.Show(); 

一定要改變文件名

+0

我想是這樣的sRingTonePath = Path.GetFullPath(ISF.GetFileNames()[0 ]);然後使用sRingTonePath作爲源,但仍然是同樣的問題。 – user2530266

+1

我以前做過這些,爲了創建應用程序,我需要它,我遵循的步驟是 1)使用HttpClient將文件下載到Stream對象中 2)在隔離存儲(Local Storage)中創建一個新文件)並將流保存在其中 3)開始一個新的RingToneTask並將Source屬性設置爲之前創建的文件。完成! –

+0

我正在做同樣的事情。我更新了這個問題。你是這個意思嗎? – user2530266