WP8.1 SaveRingtoneTask InvalidOperationException

时间:2015-08-23 14:08:15

标签: c# windows exception windows-phone-8

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

    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 个答案:

答案 0 :(得分: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();

请务必更改FileName