From ba0cffb5117a817632f9775c31abf598f771c61c Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 12 Nov 2022 11:44:20 +0400 Subject: [PATCH] android: catch DecodeException (#1351) * Catched drawable's DecodeException (cherry picked from commit 90ed170f61a661ff5459215b69a63923cf53f13f) * missing import * texts Co-authored-by: Avently <7953703+avently@users.noreply.github.com> --- .../chat/simplex/app/views/chat/ComposeView.kt | 17 ++++++++++++++--- .../app/src/main/res/values-de/strings.xml | 2 ++ .../app/src/main/res/values-ru/strings.xml | 2 ++ .../android/app/src/main/res/values/strings.xml | 2 ++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt index 62a91d6973..f6a13eda21 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt @@ -6,9 +6,11 @@ import android.content.* import android.content.pm.PackageManager import android.graphics.Bitmap import android.graphics.ImageDecoder +import android.graphics.ImageDecoder.DecodeException import android.graphics.drawable.AnimatedImageDrawable import android.net.Uri import android.provider.MediaStore +import android.util.Log import android.widget.Toast import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContract @@ -65,7 +67,7 @@ data class ComposeState( val inProgress: Boolean = false, val useLinkPreviews: Boolean ) { - constructor(editingItem: ChatItem, useLinkPreviews: Boolean): this ( + constructor(editingItem: ChatItem, useLinkPreviews: Boolean): this( editingItem.content.text, chatItemPreview(editingItem), ComposeContextItem.EditingItem(editingItem), @@ -164,8 +166,17 @@ fun ComposeView( val imagesPreview = ArrayList() uris.forEach { uri -> val source = ImageDecoder.createSource(context.contentResolver, uri) - val drawable = ImageDecoder.decodeDrawable(source) - var bitmap: Bitmap? = ImageDecoder.decodeBitmap(source) + val drawable = try { + ImageDecoder.decodeDrawable(source) + } catch (e: DecodeException) { + AlertManager.shared.showAlertMsg( + title = generalGetString(R.string.image_decoding_exception_title), + text = generalGetString(R.string.image_decoding_exception_desc) + ) + Log.e(TAG, "Error while decoding drawable: ${e.stackTraceToString()}") + null + } + var bitmap: Bitmap? = if (drawable != null) ImageDecoder.decodeBitmap(source) else null if (drawable is AnimatedImageDrawable) { // It's a gif or webp val fileSize = getFileSize(context, uri) diff --git a/apps/android/app/src/main/res/values-de/strings.xml b/apps/android/app/src/main/res/values-de/strings.xml index 303d088473..29c6ee5efd 100644 --- a/apps/android/app/src/main/res/values-de/strings.xml +++ b/apps/android/app/src/main/res/values-de/strings.xml @@ -185,6 +185,8 @@ Dateivorschau abbrechen Zu viele Bilder! Es können nur 10 Bilder auf einmal gesendet werden + ***Decoding error + ***The image cannot be decoded. Please, try a different image or contact developers. Bild diff --git a/apps/android/app/src/main/res/values-ru/strings.xml b/apps/android/app/src/main/res/values-ru/strings.xml index 1c3e258665..3ddd6de490 100644 --- a/apps/android/app/src/main/res/values-ru/strings.xml +++ b/apps/android/app/src/main/res/values-ru/strings.xml @@ -185,6 +185,8 @@ Удалить превью файла Слишком много изображений! Только 10 изображений могут быть отправлены одномоментно + Ошибка декодирования + Не получается декодировать изображение. Пожалуйста, попробуйте другое изображение или свяжитесь с разработчиками. Изображение diff --git a/apps/android/app/src/main/res/values/strings.xml b/apps/android/app/src/main/res/values/strings.xml index a1c83add72..b5f8895d77 100644 --- a/apps/android/app/src/main/res/values/strings.xml +++ b/apps/android/app/src/main/res/values/strings.xml @@ -185,6 +185,8 @@ Cancel file preview Too many images! Only 10 images can be sent at the same time + Decoding error + The image cannot be decoded. Please, try a different image or contact developers. Image