From 0aff0c1f208656265dcc54849ff6b7b311cf69a0 Mon Sep 17 00:00:00 2001 From: cketti Date: Sat, 4 Sep 2021 00:24:08 +0200 Subject: [PATCH] Use ShareCompat.IntentBuilder for sharing images ShareCompat does the right things so image previews show up in the Share sheet of Android 11. --- changelog.d/3965.bugfix | 1 + .../core/utils/ExternalApplicationsUtil.kt | 21 ++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) create mode 100644 changelog.d/3965.bugfix diff --git a/changelog.d/3965.bugfix b/changelog.d/3965.bugfix new file mode 100644 index 0000000000..9d966c2ca3 --- /dev/null +++ b/changelog.d/3965.bugfix @@ -0,0 +1 @@ +Enable image preview in Android's share sheet (Android 11+) diff --git a/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt b/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt index d0535b667f..bdaf520ba1 100644 --- a/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt +++ b/vector/src/main/java/im/vector/app/core/utils/ExternalApplicationsUtil.kt @@ -36,6 +36,7 @@ import androidx.activity.result.ActivityResultLauncher import androidx.browser.customtabs.CustomTabColorSchemeParams import androidx.browser.customtabs.CustomTabsIntent import androidx.browser.customtabs.CustomTabsSession +import androidx.core.app.ShareCompat import androidx.core.content.FileProvider import androidx.core.content.getSystemService import im.vector.app.BuildConfig @@ -297,23 +298,19 @@ fun openMedia(activity: Activity, savedMediaPath: String, mimeType: String) { } fun shareMedia(context: Context, file: File, mediaMimeType: String?) { - var mediaUri: Uri? = null - try { - mediaUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file) + val mediaUri = try { + FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", file) } catch (e: Exception) { Timber.e(e, "onMediaAction Selected File cannot be shared") + return } - if (null != mediaUri) { - val sendIntent = Intent() - sendIntent.action = Intent.ACTION_SEND - // Grant temporary read permission to the content URI - sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - sendIntent.type = mediaMimeType - sendIntent.putExtra(Intent.EXTRA_STREAM, mediaUri) + val sendIntent = ShareCompat.IntentBuilder(context) + .setType(mediaMimeType) + .setStream(mediaUri) + .getIntent() - sendShareIntent(context, sendIntent) - } + sendShareIntent(context, sendIntent) } fun shareText(context: Context, text: String) {