【发布时间】:2018-11-11 09:19:55
【问题描述】:
自定义通知声音不起作用。来自内部存储的声音,文件所有者不是我的应用程序。
代码:
Uri sound = Uri.parse(account.notifySound);
// "account.notifySound" contains path to audio file, obtained from ringtone picker
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_new_message)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pendingIntent);
if (account.notifyLedColor > 0) notificationBuilder.setLights(MainActivity.ledColorsList[account.notifyLedColor - 1], 100, 50);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(tag, 0, notificationBuilder.build());
错误:
java.lang.SecurityException: Permission Denial: reading com.android.fileexplorer.provider.FileExplorerFileProvider uri content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Marbles.mp3 from pid=1874, uid=1000 requires the provider be exported, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:616)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:483)
at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:474)
at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:419)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:313)
at android.os.Binder.execTransact(Binder.java:569)
2018-11-11 16:08:20.613 756-15193/? E/MediaPlayerService: Couldn't open fd for content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Marbles.mp3
2018-11-11 16:08:20.613 1874-23378/? E/MediaPlayer: Unable to create media player
minSdkVersion 21
如何解决?
UPD:
我在“Uri sound = Uri.parse(account.notifySound);”之后添加:
grantUriPermission("com.android.systemui", sound, Intent.FLAG_GRANT_READ_URI_PERMISSION);
得到一个错误:
java.lang.SecurityException: Uid 10204 does not have permission to uri 0 @ content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Marbles.mp3
UPD2:
铃声选择器打开了多个声音浏览器,例如“音乐”、“主题”,再次是“音乐” - 另一个声音浏览器,与第一个“音乐”不同...
当我在“音乐”中选择声音时,选择 soundUri = :
content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Snapchat_Tone-04a0409e-68c4-4294-beb3-bb137b8d5886.mp3
在“主题”中选择声音时选择 uri:
file:///storage/emulated/0/MIUI/.ringtone/Snapchat_Tone-04a0409e-68c4-4294-beb3-bb137b8d5886.mp3
当 Uri 以“file:///”(声音资源管理器“主题”)开头时播放此声音,当 Uri 以“content://”(声音资源管理器“音乐”)开头时出现错误。
虽然在“音乐”中选择时会播放一些其他文件,但 Uri 开始于“content://...”
【问题讨论】:
-
Android 现在是我见过的漏洞最多、最不可靠的操作系统...
标签: android notifications