通知中的FileUriExposedException

时间:2017-08-16 08:04:27

标签: android

我收到Crashlytics的报告:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver MY_AWERSOME_APP.NotificationsBroadcastReceiver: android.os.FileUriExposedException: file:///storage/emulated/0/Ringtones/high_priority.wav exposed beyond app through Notification.sound
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:3055)
   at android.app.ActivityThread.-wrap18(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1579)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6195)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)

Caused by android.os.FileUriExposedException: file:///storage/emulated/0/Ringtones/high_priority.wav exposed beyond app through Notification.sound
   at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
   at android.net.Uri.checkFileUriExposed(Uri.java:2346)
   at android.app.NotificationManager.notifyAsUser(NotificationManager.java:300)
   at android.app.NotificationManager.notify(NotificationManager.java:284)
   at android.app.NotificationManager.notify(NotificationManager.java:268)
   at MY_AWERSOME_APP.NotificationsBroadcastReceiver.onReceive(NotificationsBroadcastReceiver.java:105)
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:3048)
   at android.app.ActivityThread.-wrap18(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1579)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6195)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)

My NotificationsBroadcastReceiver,not full,仅针对URI:

public class NotificationsBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

    // We receive URI string from shared prefs
    String ringtoneUriString = sharedPref.getString(PreferencesHelper.PREF_NOTIFICATIONS_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString());

    // Creating uri
    Uri ringtoneUri = Uri.parse(ringtoneUriString);
    if (RingtoneManager.getRingtone(context, ringtoneUri) != null){
        ringtoneUri = Uri.parse(ringtoneUriString);
    } else {
        ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }

    Notification notification = new Notification.Builder(context)
            .setSmallIcon(R.drawable.img_notifications_small)
            .setLargeIcon(image)
            .setContentText(content)
            .setContentTitle(title)
            .setAutoCancel(true)
            .setWhen(when)
            .setLights(ledColor, 500, 2000)
            .setContentIntent(pendingIntent)
            .setSound(ringtoneUri)
            .setVibrate(vibrateValue)
            .build();

    int id = 1;
    notificationManager.notify(id, notification);
}
}

用户通过以下方式选择URI:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.pref_notifications_ringtone_choose_title));
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtoneUri);
                startActivityForResult(intent, RINGTONE_REQUEST_CODE);

onActivityResult:

if (requestCode == RINGTONE_REQUEST_CODE && resultCode == Activity.RESULT_OK){

        mRingtoneString = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI).toString();

        sharedPreferences.edit().putString(
                PreferencesHelper.PREF_NOTIFICATIONS_URI,
                mRingtoneString
        ).apply();

        Uri ringtoneUri = Uri.parse(mRingtoneString);
        if (RingtoneManager.getRingtone(this, ringtoneUri) != null){
            Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
            ringtoneNameTextView.setText(ringtone.getTitle(this));
        } else {
            ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
            ringtoneNameTextView.setText(ringtone.getTitle(this));
        }
    }
清单中的

permisions

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

minSdkVersion 22

targetSdkVersion 26

我无法在任何地方重现此错误。 但我会定期收到关于此错误的定期报告

可以做些什么呢?上下挖掘这个网站,但找不到合适的信息。

如果出现问题,谢谢你,对不起!

0 个答案:

没有答案