android Ringtone API,为什么RingtoneManager.setActualDefaultRingtoneUri无法正常工作? [android API-8]...

这篇博客介绍了如何处理Uri,尤其是当试图将一个文件Uri设置为设备的默认铃声时遇到的问题。文章指出,不能直接使用文件Uri,而必须通过ContentResolver插入内容值。代码示例展示了如何创建ContentValues,删除已存在的铃声,然后插入新的Uri,最后设置为默认铃声。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

好的,我和你有同样的问题。我假设您传入的Uri是您资产中的文件,或者是您存储在首选项文件中的磁盘。在这个假设下工作,问题是你不能将Uri推入RingtoneManager并期望它接受它。文件uri应该来自内容解析器。

我很乐意,如果有人能告诉我为什么会这样,但我不是专家,所以我现在就接受它。话虽这么说,这段代码将让你采取一个Uri并将其设置为默认铃声。

//We get the Uri here fro ma file's absolute path.

Uri ringtoneUri = Uri.parse(file.getAbsolutePath());

//We now create a new content values object to store all the information

//about the ringtone.

ContentValues values = new ContentValues();

values.put(MediaStore.MediaColumns.DATA, chosenFile.getAbsolutePath());

values.put(MediaStore.MediaColumns.TITLE, chosenFile.getName());

values.put(MediaStore.MediaColumns.SIZE, chosenFile.length());

values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");

values.put(AudioColumns.ARTIST, context.getString(R.string.app_name));

values.put(AudioColumns.IS_RINGTONE, true);

values.put(AudioColumns.IS_NOTIFICATION, false);

values.put(AudioColumns.IS_ALARM, false);

values.put(AudioColumns.IS_MUSIC, false);

//Work with the content resolver now

//First get the file we may have added previously and delete it,

//otherwise we will fill up the ringtone manager with a bunch of copies over time.

Uri uri = MediaStore.Audio.Media.getContentUriForPath(chosenFile.getAbsolutePath());

context.getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + chosenFile.getAbsolutePath() + "\"", null);

//Ok now insert it

Uri newUri = context.getContentResolver().insert(uri, values);

//Ok now set the ringtone from the content manager's uri, NOT the file's uri

RingtoneManager.setActualDefaultRingtoneUri(

context,

RingtoneManager.TYPE_RINGTONE,

newUri

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值