From c793ef34cadee36e272a798f9d2d2e4dfbf1453b Mon Sep 17 00:00:00 2001 From: Adolfo Santiago Date: Sat, 16 Mar 2024 16:40:23 +0100 Subject: [PATCH] Control if there is an app to open images It'll show a Toast message if there are not applications to open the image. The image must be downloaded first in order to open it in external apps. --- .../keylesspalace/tusky/ViewMediaActivity.kt | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/husky/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt b/husky/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt index a598284a..e19e9a42 100644 --- a/husky/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt +++ b/husky/app/src/main/java/com/keylesspalace/tusky/ViewMediaActivity.kt @@ -24,6 +24,7 @@ import android.Manifest import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.app.DownloadManager +import android.content.ActivityNotFoundException import android.content.ClipData import android.content.ClipboardManager import android.content.Context @@ -345,24 +346,27 @@ class ViewMediaActivity : BaseActivity(), ViewImageFragment.PhotoActionsListener } private fun openInExternalApp() { - val url = avatarUrl ?: attachments!![binding.viewPager.currentItem].attachment.url - val intent = Intent(Intent.ACTION_VIEW) - val extension = MimeTypeMap.getFileExtensionFromUrl(url) - if (extension != null) { - intent.setDataAndType( - Uri.parse(url), - MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) - ) - } else { - intent.data = Uri.parse(url) + val directory = applicationContext.getExternalFilesDir("Husky") + if (directory == null || !(directory.exists())) { + Toast.makeText( + this, + "Cannot open this in an external application.", + Toast.LENGTH_LONG + ).show() + + return } - startActivity(intent) + shareImage( + directory, + avatarUrl ?: attachments!![binding.viewPager.currentItem].attachment.url, + true + ) } private var isCreating: Boolean = false - private fun shareImage(directory: File, url: String) { + private fun shareImage(directory: File, url: String, openExternal: Boolean = false) { isCreating = true binding.progressBarShare.visibility = View.VISIBLE invalidateOptionsMenu() @@ -396,7 +400,11 @@ class ViewMediaActivity : BaseActivity(), ViewImageFragment.PhotoActionsListener invalidateOptionsMenu() binding.progressBarShare.visibility = View.GONE if (result) { - shareFile(file, "image/png") + if (openExternal) { + openImageInExternalApp(file) + } else { + shareFile(file, "image/png") + } } }, { error -> @@ -424,6 +432,30 @@ class ViewMediaActivity : BaseActivity(), ViewImageFragment.PhotoActionsListener shareFile(file, mimeType) } + + private fun openImageInExternalApp(file: File) { + try { + startActivity( + Intent().apply { + action = Intent.ACTION_VIEW + setDataAndType( + FileProvider.getUriForFile( + applicationContext, + "$APPLICATION_ID.fileprovider", + file + ), + "image/png" + ) + } + ) + } catch (e: ActivityNotFoundException) { + Toast.makeText( + this, + "No app available to open images", + Toast.LENGTH_LONG + ).show() + } + } } abstract class ViewMediaAdapter(activity: FragmentActivity) : FragmentStateAdapter(activity) {