Prevent out of memory exception when receiving a particular image causing Coil to allocate a 430Mo bitmap

This commit is contained in:
Sylvain Berfini 2024-01-15 14:26:44 +01:00
parent 7b01ceb6b9
commit 715be39669
3 changed files with 44 additions and 4 deletions

View file

@ -230,7 +230,7 @@ dependencies {
implementation 'com.google.android.flexbox:flexbox:3.0.0'
// https://github.com/coil-kt/coil/blob/main/LICENSE.txt Apache v2.0
def coil_version = "2.4.0"
def coil_version = "2.5.0"
implementation("io.coil-kt:coil:$coil_version")
implementation("io.coil-kt:coil-gif:$coil_version")
implementation("io.coil-kt:coil-svg:$coil_version")

View file

@ -44,6 +44,7 @@ import coil.dispose
import coil.load
import coil.request.CachePolicy
import coil.request.videoFrameMillis
import coil.size.Dimension
import coil.transform.CircleCropTransformation
import com.google.android.material.switchmaterial.SwitchMaterial
import kotlinx.coroutines.*
@ -353,11 +354,39 @@ fun loadRoundImageWithCoil(imageView: ImageView, path: String?) {
}
}
@BindingAdapter("coilBubble")
fun loadImageInChatBubbleWithCoil(imageView: ImageView, path: String?) {
val height = Dimension(
imageView.resources.getDimension(
R.dimen.chat_message_bubble_image_height_big
).toInt()
)
val width = Dimension.Undefined
loadImageWithCoil(imageView, path, width, height)
}
@BindingAdapter("coilGrid")
fun loadImageInChatGridBubbleWithCoil(imageView: ImageView, path: String?) {
val size = Dimension(
imageView.resources.getDimension(
R.dimen.chat_message_bubble_file_size
).toInt()
)
loadImageWithCoil(imageView, path, size, size)
}
@BindingAdapter("coil")
fun loadImageWithCoil(imageView: ImageView, path: String?) {
loadImageWithCoil(imageView, path, Dimension.Undefined, Dimension.Undefined)
}
fun loadImageWithCoil(imageView: ImageView, path: String?, width: Dimension, height: Dimension) {
if (!path.isNullOrEmpty() && FileUtils.isExtensionImage(path)) {
if (corePreferences.vfsEnabled && path.startsWith(corePreferences.vfsCachePath)) {
imageView.load(path) {
size(width, height)
diskCachePolicy(CachePolicy.DISABLED)
listener(
onError = { _, result ->
@ -369,6 +398,7 @@ fun loadImageWithCoil(imageView: ImageView, path: String?) {
}
} else {
imageView.load(path) {
size(width, height)
listener(
onError = { _, result ->
Log.e("[Data Binding] [Coil] Error loading [$path]: ${result.throwable}")

View file

@ -31,9 +31,19 @@
android:layout_height="wrap_content"
app:layout_constraintHeight_max="@dimen/chat_message_bubble_image_height_big"
app:layout_constraintHeight_min="@dimen/chat_message_bubble_image_height_small"
android:layout_size="@{data.alone ? 0f : @dimen/chat_message_bubble_file_size}"
coil="@{data.filePath}"
android:scaleType="@{data.alone ? ScaleType.FIT_CENTER : ScaleType.CENTER_CROP}"
coilBubble="@{data.filePath}"
android:visibility="@{data.alone ? View.VISIBLE : View.GONE}"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
tools:ignore="MissingConstraints" />
<ImageView
android:contentDescription="@string/content_description_downloaded_file_transfer"
android:layout_width="@dimen/chat_message_bubble_file_size"
android:layout_height="@dimen/chat_message_bubble_file_size"
coilGrid="@{data.filePath}"
android:visibility="@{data.alone ? View.GONE : View.VISIBLE}"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
tools:ignore="MissingConstraints" />