From 7efec030561c9cc42771a04a86e7501d22da19dd Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Mon, 12 May 2025 22:26:24 +0200 Subject: [PATCH] Return file extension from ShareHelper just in case mimetype is null Use content to determine whether it's an image --- .../amethyst/ui/components/ShareHelper.kt | 7 +++---- .../ui/components/ZoomableContentView.kt | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt index b248959a5..c3e24ff3c 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt @@ -36,8 +36,7 @@ object ShareHelper { fun getSharableUriFromUrl( context: Context, imageUrl: String, - mimeType: String, - ): Uri { + ): Pair { try { // Safely get snapshot and file val snapshot = @@ -53,7 +52,7 @@ object ShareHelper { val fileCopy = prepareSharableImageFile(context, file, fileExtension) // Return sharable uri - return getSharableUri(context, fileCopy) + return Pair(getSharableUri(context, fileCopy), fileExtension) } } catch (e: IOException) { Log.e("ShareHelper", "Error sharing image", e) @@ -61,7 +60,7 @@ object ShareHelper { } } - private fun getImageExtension(file: File): String = + fun getImageExtension(file: File): String = try { FileInputStream(file).use { inputStream -> val header = ByteArray(12) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt index a944d6c93..6f73b1b68 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt @@ -685,6 +685,7 @@ fun ShareImageAction( hash = content.hash, mimeType = content.mimeType, onDismiss = onDismiss, + content = content, ) } else if (content is MediaPreloadedContent) { ShareImageAction( @@ -713,6 +714,7 @@ fun ShareImageAction( hash: String?, mimeType: String?, onDismiss: () -> Unit, + content: BaseMediaContent? = null, ) { DropdownMenu( expanded = popupExpanded.value, @@ -757,16 +759,20 @@ fun ShareImageAction( ) } - mimeType?.let { - if (mimeType.startsWith("image")) { + content?.let { + if (content is MediaUrlImage) { val context = LocalContext.current videoUri?.let { if (videoUri.isNotEmpty()) { DropdownMenuItem( text = { Text(stringRes(R.string.share_image)) }, onClick = { - val uri = ShareHelper.getSharableUriFromUrl(context, videoUri, mimeType) - shareMediaFile(context, uri, mimeType) + val (uri, fileExtension) = ShareHelper.getSharableUriFromUrl(context, videoUri) + if (mimeType == null) { + shareMediaFile(context, uri, "image/$fileExtension") + } else { + shareMediaFile(context, uri, mimeType) + } onDismiss() }, ) @@ -780,7 +786,7 @@ fun ShareImageAction( private fun shareMediaFile( context: Context, uri: Uri, - mimeType: String = "image/*", + mimeType: String, ) { val shareIntent = Intent(Intent.ACTION_SEND).apply { @@ -790,7 +796,7 @@ private fun shareMediaFile( } // TODO is this the right scope to avoid leaks? CoroutineScope(Dispatchers.Main).launch { - context.startActivity(Intent.createChooser(shareIntent, "Share Image")) + context.startActivity(Intent.createChooser(shareIntent, null)) } }