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(
context: Context,
imageUrl: String,
mimeType: String,
): Uri {
): Pair<Uri, String> {
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)
@@ -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))
}
}