Return file extension from ShareHelper just in case mimetype is null

Use content to determine whether it's an image
This commit is contained in:
David Kaspar
2025-05-12 22:26:24 +02:00
parent 91b6bf6e88
commit 7efec03056
2 changed files with 15 additions and 10 deletions
@@ -36,8 +36,7 @@ object ShareHelper {
fun getSharableUriFromUrl( fun getSharableUriFromUrl(
context: Context, context: Context,
imageUrl: String, imageUrl: String,
mimeType: String, ): Pair<Uri, String> {
): Uri {
try { try {
// Safely get snapshot and file // Safely get snapshot and file
val snapshot = val snapshot =
@@ -53,7 +52,7 @@ object ShareHelper {
val fileCopy = prepareSharableImageFile(context, file, fileExtension) val fileCopy = prepareSharableImageFile(context, file, fileExtension)
// Return sharable uri // Return sharable uri
return getSharableUri(context, fileCopy) return Pair(getSharableUri(context, fileCopy), fileExtension)
} }
} catch (e: IOException) { } catch (e: IOException) {
Log.e("ShareHelper", "Error sharing image", e) 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 { try {
FileInputStream(file).use { inputStream -> FileInputStream(file).use { inputStream ->
val header = ByteArray(12) val header = ByteArray(12)
@@ -685,6 +685,7 @@ fun ShareImageAction(
hash = content.hash, hash = content.hash,
mimeType = content.mimeType, mimeType = content.mimeType,
onDismiss = onDismiss, onDismiss = onDismiss,
content = content,
) )
} else if (content is MediaPreloadedContent) { } else if (content is MediaPreloadedContent) {
ShareImageAction( ShareImageAction(
@@ -713,6 +714,7 @@ fun ShareImageAction(
hash: String?, hash: String?,
mimeType: String?, mimeType: String?,
onDismiss: () -> Unit, onDismiss: () -> Unit,
content: BaseMediaContent? = null,
) { ) {
DropdownMenu( DropdownMenu(
expanded = popupExpanded.value, expanded = popupExpanded.value,
@@ -757,16 +759,20 @@ fun ShareImageAction(
) )
} }
mimeType?.let { content?.let {
if (mimeType.startsWith("image")) { if (content is MediaUrlImage) {
val context = LocalContext.current val context = LocalContext.current
videoUri?.let { videoUri?.let {
if (videoUri.isNotEmpty()) { if (videoUri.isNotEmpty()) {
DropdownMenuItem( DropdownMenuItem(
text = { Text(stringRes(R.string.share_image)) }, text = { Text(stringRes(R.string.share_image)) },
onClick = { onClick = {
val uri = ShareHelper.getSharableUriFromUrl(context, videoUri, mimeType) val (uri, fileExtension) = ShareHelper.getSharableUriFromUrl(context, videoUri)
shareMediaFile(context, uri, mimeType) if (mimeType == null) {
shareMediaFile(context, uri, "image/$fileExtension")
} else {
shareMediaFile(context, uri, mimeType)
}
onDismiss() onDismiss()
}, },
) )
@@ -780,7 +786,7 @@ fun ShareImageAction(
private fun shareMediaFile( private fun shareMediaFile(
context: Context, context: Context,
uri: Uri, uri: Uri,
mimeType: String = "image/*", mimeType: String,
) { ) {
val shareIntent = val shareIntent =
Intent(Intent.ACTION_SEND).apply { Intent(Intent.ACTION_SEND).apply {
@@ -790,7 +796,7 @@ private fun shareMediaFile(
} }
// TODO is this the right scope to avoid leaks? // TODO is this the right scope to avoid leaks?
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
context.startActivity(Intent.createChooser(shareIntent, "Share Image")) context.startActivity(Intent.createChooser(shareIntent, null))
} }
} }