Android 7.0 Notification Sound from Uri file provider not playing

I am changing the application code to support Android 7, but in my NotificationCompat.Builder.setSound (Uri), passing the Uri from FileProvider, Notification does not play any sound, in Android 6 using Uri.fromFile () worked correctly.

The mp3 file is located in:

/Animeflv/cache/.sounds/

This is my notification code:

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

This is my 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);
                }
        }
    }

In AndroidManifest.xml:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="knf.animeflv.RequestsBackground"
    android:exported="false"
    android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</provider>

provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="_.sounds" path="Animeflv/cache/.sounds/"/>
</paths>
+3
source share
1 answer

, , , , , ?


Notification, , setSound() NotificationCompat.Builder. Uri, Android 7.0, .

file: Uri, Android 7.0 targetSdkVersion 24 , Uri file: Uri.

, content: Uri, , FileProvider, ... Android .

.

: grantUriPermissions()

grantUriPermissions(), Context. , , .

Nexus 6P (Android 6.0... ...) Nexus 9 (Android 7.0):

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

( sound - Uri, setSound())

Android, .

:

android.resource, Uri setSound(). , , , . , , .

: ContentProvider

FileProvider — . content: Uri, - , exported (, , , com.android.systemui, ).

, my StreamProvider, " ".

.

:

StrictMode, VM (.. , ) file: Uri:

StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());

VmPolicy , , detectFileUriExposure().

file: Uri . , Google file: Uri, .

The Nuke: targetSdkVersion

file: Uri, , a targetSdkVersion 24+. , " " Toast .

: Android

NotificationManager grantUriPermissions() , - FLAG_GRANT_READ_URI_PERMISSION Uri, Notification . .

+12

Source: https://habr.com/ru/post/1653397/


All Articles