Android:将 SD 卡中的声音设置为铃声

发布于 2024-10-16 08:12:06 字数 1161 浏览 3 评论 0原文

我正在尝试从应用程序中保存到 SD 卡上的声音设置铃声。在浏览完我能找到的关于此问题的每一篇文章后,我相信我已经很接近了,只是没有定义 URI 的正确性。

       File ringPath = new File(path, filename);

       ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, ringPath.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "temptitle");
       values.put(MediaStore.MediaColumns.SIZE, ringPath.length());
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
       values.put(MediaStore.Audio.Media.ARTIST, "tempartist");
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

       uri = Uri.fromFile(ringPath);
       RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);

通常,URI 的定义更像是:

Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringPath.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

但我并没有完全明白这一切的意义,它给了我相当严重的错误(并不是说它们可能比我当前的错误更糟糕)。我确信这是执行此操作所需的方法,但是我的代码中的 uri 实际上返回了正确的路径(而另一个则没有,可能是由于我的误用),所以我不明白为什么它不会工作。虽然说实话,我不认为必须使用 URI 来实现此目的,而不是指定路径。

如果有人能给我一些解释,或者将我链接到某个地方,这样我就能真正理解这一点并解决这个问题,我将非常感激。

I am trying to set a ringtone from a sound I saved onto the SD card from within my app. After going through every post I could find on this, I believe I am close, and just not defining the URI's right.

       File ringPath = new File(path, filename);

       ContentValues values = new ContentValues();
       values.put(MediaStore.MediaColumns.DATA, ringPath.getAbsolutePath());
       values.put(MediaStore.MediaColumns.TITLE, "temptitle");
       values.put(MediaStore.MediaColumns.SIZE, ringPath.length());
       values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
       values.put(MediaStore.Audio.Media.ARTIST, "tempartist");
       values.put(MediaStore.Audio.Media.IS_RINGTONE, true);

       uri = Uri.fromFile(ringPath);
       RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, uri);

Typically the URI is defined more like :

Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringPath.getAbsolutePath());
Uri newUri = main.getContentResolver().insert(uri, values);

But I didn't quite see the point in all of that, and it gave me rather nasty errors (Not that they were likely any worse than my current one). I am sure that is the required way to do it, but the uri in my code actually returns the proper path (And the other doesn't, granted likely due to my misuse), so I don't see why it wouldn't work. Though to be honest I don't see the point in having to using URI's for this anyway, rather than specifying the paths.

If anybody could give me a little explanation, or link me somewhere that would, so I could really comprehend this and get around this issue I'd be very grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-10-23 08:12:06

通过进入菜单->设置->应用程序->开发来关闭电话调试,然后取消选中该框。很好,可以走了。

Shut off phone debugging by going into Menu->Settings->Applications->Development Then uncheck the box. Good to go.

谁许谁一生繁华 2024-10-23 08:12:06

我认为你的问题是没有找到路径。因此,我建议您首先检查您的文件在这一行之后是否确实存在

 File ringPath = new File(path, filename);

使用代码:

boolean b=ringPath.exists();

如果它返回 true 则您可以从中获取 uri。然后我认为在你的代码中没有看到错误。

I think your problem is not getting path. So I would recommend you to start with check if your file is really exists after this line

 File ringPath = new File(path, filename);

Use code:

boolean b=ringPath.exists();

If it returns true than you can able to get uri from it. Then after I think no error I can see in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文