android7.0无声音,来自文件提供商Uri的Android 7.0通知声音未播放

在升级应用程序以支持Android7时遇到问题,通知的自定义声音无法通过FileProvider在Android7上播放,而在Android6中使用Uri.fromFile()则正常。代码中尝试通过UtilSound.getSoundUri获取Uri,但FileProvider的配置可能存在问题。

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

我正在更改应用程序代码以支持Android

7,但是在我的NotificationCompat.Builder.setSound(Uri)中,从FileProvider传递Uri时,通知没有播放声音,在Android

6中,使用Uri.fromFile()可以正常工作。

mp3文件位于:

/ Animeflv /缓存/.sounds/

这是我的通知代码:

knf.animeflv.RequestBackground

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)

.setSmallIcon(R.drawable.ic_not_r)

.setContentTitle(NotTit)

.setContentText(mess);

...

mBuilder.setVibrate(new long[]{100, 200, 100, 500});

mBuilder.setSound(UtilSound.getSoundUri(not)); //int

这是我的UtilSound.getSoundUri(int)

public static Uri getSoundUri(int not) {

switch (not) {

case 0:

return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

default:

try {

File file=new File(Environment.getExternalStorageDirectory()+"/Animeflv/cache/.sounds",getSoundsFileName(not));

if (file.exists()) {

file.setReadable(true,false);

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){

return FileProvider.getUriForFile(context, "knf.animeflv.RequestsBackground",file);

}else {

return Uri.fromFile(file);

}

}else {

Log.d("Sound Uri","Not found");

return getSoundUri(0);

}

}catch (Exception e){

e.printStackTrace();

return getSoundUri(0);

}

}

}

在AndroidManifest.xml中:

android:name="android.support.v4.content.FileProvider"

android:authorities="knf.animeflv.RequestsBackground"

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/provider_paths"/>

provider_paths.xml:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值