Notification sound not played. Permission Denial

Viewed 1299

Custom notify sound not working. Sound from internal storage, file owner not my app.

Code:

    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());

Error:

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

How to fix it?

UPD:

I added after "Uri sound = Uri.parse(account.notifySound);":

 grantUriPermission("com.android.systemui", sound, Intent.FLAG_GRANT_READ_URI_PERMISSION); 

And got an error:

 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:

Ringtone chooser opened multiple sound explorer, eg "Music", "Themes", again "Music" - another sound explorer, different on first "Music" ...

And when i select sound in "Music", picked soundUri = :

content://com.android.fileexplorer.myprovider/external_files/zedge/notification_sound/Snapchat_Tone-04a0409e-68c4-4294-beb3-bb137b8d5886.mp3

When select sound in "Themes" picked uri:

file:///storage/emulated/0/MIUI/.ringtone/Snapchat_Tone-04a0409e-68c4-4294-beb3-bb137b8d5886.mp3

This sound plays when it Uri begins with "file:///" (sound explorer "Themes") and got error when Uri begins "content://" (sound explorer "Music").

Although some other files play when choosing in "Music" with Uri begins "content://..."

1 Answers

As compiler said:

requires the provider be exported, or grantUriPermission()

You could try this:

grantUriPermission("com.android.systemui", sound,
    Intent.FLAG_GRANT_READ_URI_PERMISSION);

where sound is the Uri that you are using with setSound().

If you can not sure which packages should grant permission, below helper functional would loop search:

public static void grantUriPermission(Activity ctx, Intent intent, Uri uri, int permissionFlags) {
    List<ResolveInfo> resolvedIntentActivities = ctx.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
        String packageName = resolvedIntentInfo.activityInfo.packageName;

        ctx.grantUriPermission(packageName, uri, permissionFlags);
    }
}
Related