From 9d3e3e21df4c4f5aa494cd226342ac275bca3e88 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Wed, 7 Feb 2024 13:29:44 +0100 Subject: [PATCH] Send uri path directly and add try/catch to prevent permission related crashes --- .../ui/ShareIntentReceiverActivity.java | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java index c927dbad1b9a..d16add4858d0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java @@ -4,7 +4,6 @@ import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; -import android.text.TextUtils; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -22,6 +21,8 @@ import org.wordpress.android.ui.main.WPMainActivity; import org.wordpress.android.ui.media.MediaBrowserActivity; import org.wordpress.android.ui.media.MediaBrowserType; +import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.AppLog.T; import org.wordpress.android.util.FluxCUtils; import org.wordpress.android.util.MediaUtils; import org.wordpress.android.util.ToastUtils; @@ -95,27 +96,27 @@ private void refreshContent() { } private void downloadExternalMedia() { - if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction())) { - ArrayList externalUris = getIntent().getParcelableArrayListExtra((Intent.EXTRA_STREAM)); - for (Uri uri : externalUris) { - if (uri != null && isAllowedMediaType(uri)) { - mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, uri)); + try { + if (Intent.ACTION_SEND_MULTIPLE.equals(getIntent().getAction())) { + ArrayList externalUris = getIntent().getParcelableArrayListExtra((Intent.EXTRA_STREAM)); + for (Uri uri : externalUris) { + if (uri != null && isAllowedMediaType(uri)) { + mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, uri)); + } + } + } else if (Intent.ACTION_SEND.equals(getIntent().getAction())) { + Uri externalUri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); + if (externalUri != null && isAllowedMediaType(externalUri)) { + mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, externalUri)); } } - } else if (Intent.ACTION_SEND.equals(getIntent().getAction())) { - Uri externalUri = getIntent().getParcelableExtra(Intent.EXTRA_STREAM); - if (externalUri != null && isAllowedMediaType(externalUri)) { - mLocalMediaUris.add(MediaUtils.downloadExternalMedia(this, externalUri)); - } + } catch (Exception e) { + AppLog.e(T.MEDIA, "ShareIntentReceiver failed ", e); } } private boolean isAllowedMediaType(@NonNull Uri uri) { - String filePath = MediaUtils.getRealPathFromURI(this, uri); - // For cases when the uri is already the file path - if (TextUtils.isEmpty(filePath)) { - filePath = String.valueOf(uri); - } + String filePath = uri.getPath(); return MediaUtils.isValidImage(filePath) || MediaUtils.isVideo(filePath); }