From b6162c14965b8bc0b379771f47aa1abba7b8e80d Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Sat, 11 Jun 2022 21:01:33 +0800 Subject: Fix app crashes when deleting media from notification. --- .../DownloadedMediaDeleteActionBroadcastReceiver.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java index 22e2e2f1..323c78eb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java @@ -8,13 +8,27 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; +import androidx.documentfile.provider.DocumentFile; + +import java.io.File; + public class DownloadedMediaDeleteActionBroadcastReceiver extends BroadcastReceiver { public static final String EXTRA_NOTIFICATION_ID = "ENI"; @Override public void onReceive(Context context, Intent intent) { Uri mediaUri = intent.getData(); if (mediaUri != null) { - context.getContentResolver().delete(mediaUri, null, null); + try { + context.getContentResolver().delete(mediaUri, null, null); + } catch (Exception e) { + DocumentFile file = DocumentFile.fromSingleUri(context, mediaUri); + if (file != null) { + if (!file.delete()) { + new File(mediaUri.toString()).delete(); + } + } + } + } NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); -- cgit v1.2.3