Skip to content

Commit

Permalink
Control if there is an app to open images
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
captainepoch committed Mar 16, 2024
1 parent ae23f1b commit c793ef3
Showing 1 changed file with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c793ef3

Please sign in to comment.