crop too tall mms attachments

This commit is contained in:
tibbi
2020-04-12 21:43:42 +02:00
parent 8827e21178
commit aecaec8773
4 changed files with 29 additions and 13 deletions

View File

@ -5,6 +5,7 @@ import android.app.PendingIntent
import android.content.Intent import android.content.Intent
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.media.MediaMetadataRetriever
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
@ -88,17 +89,24 @@ class ThreadActivity : SimpleActivity() {
messages.filter { it.attachment != null }.forEach { messages.filter { it.attachment != null }.forEach {
it.attachment!!.attachments.forEach { it.attachment!!.attachments.forEach {
try { try {
if (it.type.startsWith("image/")) {
val fileOptions = BitmapFactory.Options() val fileOptions = BitmapFactory.Options()
fileOptions.inJustDecodeBounds = true fileOptions.inJustDecodeBounds = true
BitmapFactory.decodeStream(contentResolver.openInputStream(it.uri), null, fileOptions) BitmapFactory.decodeStream(contentResolver.openInputStream(it.uri), null, fileOptions)
it.width = fileOptions.outWidth it.width = fileOptions.outWidth
it.height = fileOptions.outHeight it.height = fileOptions.outHeight
} else if (it.type.startsWith("video/")) {
val metaRetriever = MediaMetadataRetriever()
metaRetriever.setDataSource(this, it.uri)
it.width = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH).toInt()
it.height = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT).toInt()
}
if (it.width == -1) { if (it.width < 0) {
it.width = 0 it.width = 0
} }
if (it.height == -1) { if (it.height < 0) {
it.height = 0 it.height = 0
} }
} catch (ignored: Exception) { } catch (ignored: Exception) {

View File

@ -9,6 +9,7 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.RoundedCorners import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
@ -198,11 +199,13 @@ class ThreadAdapter(
val type = attachment.type val type = attachment.type
if (type.startsWith("image/") || type.startsWith("video/")) { if (type.startsWith("image/") || type.startsWith("video/")) {
val uri = attachment.uri val uri = attachment.uri
val isTallImage = attachment.height > attachment.width
val transformation = if (isTallImage) CenterCrop() else FitCenter()
val options = RequestOptions() val options = RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE) .diskCacheStrategy(DiskCacheStrategy.NONE)
.transform(FitCenter(), RoundedCorners(roundedCornersRadius)) .transform(transformation, RoundedCorners(roundedCornersRadius))
Glide.with(context) var builder = Glide.with(context)
.load(uri) .load(uri)
.transition(DrawableTransitionOptions.withCrossFade()) .transition(DrawableTransitionOptions.withCrossFade())
.apply(options) .apply(options)
@ -215,10 +218,13 @@ class ThreadAdapter(
override fun onResourceReady(dr: Drawable?, a: Any?, t: Target<Drawable>?, d: DataSource?, i: Boolean) = override fun onResourceReady(dr: Drawable?, a: Any?, t: Target<Drawable>?, d: DataSource?, i: Boolean) =
false false
}) })
.into(imageView.attachment_image)
if (isTallImage) {
builder = builder.override(attachment.width, attachment.width)
}
builder.into(imageView.attachment_image)
attachment_image.setOnClickListener { attachment_image.setOnClickListener {
Intent().apply { Intent().apply {
action = Intent.ACTION_VIEW action = Intent.ACTION_VIEW

View File

@ -38,7 +38,8 @@
android:layout_height="@dimen/play_outline_size" android:layout_height="@dimen/play_outline_size"
android:layout_alignEnd="@+id/thread_mesage_attachments_holder" android:layout_alignEnd="@+id/thread_mesage_attachments_holder"
android:layout_alignBottom="@+id/thread_mesage_attachments_holder" android:layout_alignBottom="@+id/thread_mesage_attachments_holder"
android:layout_margin="@dimen/medium_margin" android:layout_marginStart="@dimen/medium_margin"
android:layout_marginBottom="@dimen/activity_margin"
android:src="@drawable/ic_play_outline" android:src="@drawable/ic_play_outline"
android:visibility="gone" /> android:visibility="gone" />

View File

@ -29,7 +29,8 @@
android:layout_height="@dimen/play_outline_size" android:layout_height="@dimen/play_outline_size"
android:layout_alignEnd="@+id/thread_mesage_attachments_holder" android:layout_alignEnd="@+id/thread_mesage_attachments_holder"
android:layout_alignBottom="@+id/thread_mesage_attachments_holder" android:layout_alignBottom="@+id/thread_mesage_attachments_holder"
android:layout_margin="@dimen/medium_margin" android:layout_marginEnd="@dimen/medium_margin"
android:layout_marginBottom="@dimen/activity_margin"
android:src="@drawable/ic_play_outline" android:src="@drawable/ic_play_outline"
android:visibility="gone" /> android:visibility="gone" />