WP8.1 SaveRingtoneTask InvalidOperationException

本文关键字:InvalidOperationException SaveRingtoneTask WP8 | 更新日期: 2023-09-27 18:09:15

我正在尝试以编程方式设置手机的铃声。通过一些搜索,我找到了这个代码

    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;
                }
            }
        }

WP8.1 SaveRingtoneTask InvalidOperationException

你使用的路径是无效的,你应该做的是下载你想要的音乐文件在你的独立存储,然后指向路径作为一个源。

不,没有其他方法可以设置铃声。

如果你只是需要一个方法下载铃声使用这个应用程序

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();

一定要更改FileName

相关文章:
  • 没有找到相关文章