Fix crashes from large images (#474)

This commit is contained in:
Philipp Heckel 2022-11-27 12:38:07 -05:00
parent 2e7c6409ff
commit d09fdb2ff2
3 changed files with 15 additions and 9 deletions

View file

@ -173,8 +173,9 @@ class DetailAdapter(private val activity: Activity, private val lifecycleScope:
} }
val attachment = notification.attachment val attachment = notification.attachment
val image = attachment.contentUri != null && supportedImage(attachment.type) && previewableImage(attachmentFileStat) val image = attachment.contentUri != null && supportedImage(attachment.type) && previewableImage(attachmentFileStat)
maybeRenderAttachmentImage(context, attachment, image) val bitmap = if (image) attachment.contentUri?.readBitmapFromUriOrNull(context) else null
maybeRenderAttachmentBox(context, notification, attachment, attachmentFileStat, image) maybeRenderAttachmentImage(context, bitmap)
maybeRenderAttachmentBox(context, notification, attachment, attachmentFileStat, bitmap)
} }
private fun maybeRenderIcon(context: Context, notification: Notification, iconStat: FileInfo?) { private fun maybeRenderIcon(context: Context, notification: Notification, iconStat: FileInfo?) {
@ -238,8 +239,8 @@ class DetailAdapter(private val activity: Activity, private val lifecycleScope:
return button return button
} }
private fun maybeRenderAttachmentBox(context: Context, notification: Notification, attachment: Attachment, attachmentFileStat: FileInfo?, image: Boolean) { private fun maybeRenderAttachmentBox(context: Context, notification: Notification, attachment: Attachment, attachmentFileStat: FileInfo?, bitmap: Bitmap?) {
if (image) { if (bitmap != null) {
attachmentBoxView.visibility = View.GONE attachmentBoxView.visibility = View.GONE
return return
} }
@ -348,13 +349,12 @@ class DetailAdapter(private val activity: Activity, private val lifecycleScope:
} }
} }
private fun maybeRenderAttachmentImage(context: Context, attachment: Attachment, image: Boolean) { private fun maybeRenderAttachmentImage(context: Context, bitmap: Bitmap?) {
if (!image) { if (bitmap == null) {
attachmentImageView.visibility = View.GONE attachmentImageView.visibility = View.GONE
return return
} }
try { try {
val bitmap = attachment.contentUri?.readBitmapFromUri(context) ?: throw Exception("uri empty")
attachmentImageView.setImageBitmap(bitmap) attachmentImageView.setImageBitmap(bitmap)
attachmentImageView.setOnClickListener { attachmentImageView.setOnClickListener {
val loadImage = { view: ImageView, image: Bitmap -> view.setImageBitmap(image) } val loadImage = { view: ImageView, image: Bitmap -> view.setImageBitmap(image) }

View file

@ -419,11 +419,16 @@ fun View.ripple(scope: CoroutineScope) {
} }
} }
fun Uri.readBitmapFromUri(context: Context): Bitmap { fun Uri.readBitmapFromUri(context: Context): Bitmap {
val resolver = context.applicationContext.contentResolver val resolver = context.applicationContext.contentResolver
val bitmapStream = resolver.openInputStream(this) val bitmapStream = resolver.openInputStream(this)
return BitmapFactory.decodeStream(bitmapStream) val bitmap = BitmapFactory.decodeStream(bitmapStream)
if (bitmap.byteCount > 100 * 1024 * 1024) {
// If the Bitmap is too large to be rendered (100 MB), it will throw a RuntimeException downstream.
// This workaround throws a catchable exception instead. See issue #474. From https://stackoverflow.com/a/53334563/1440785
throw Exception("Bitmap too large to draw on Canvas (${bitmap.byteCount} bytes)")
}
return bitmap
} }
fun String.readBitmapFromUri(context: Context): Bitmap { fun String.readBitmapFromUri(context: Context): Bitmap {

View file

@ -9,6 +9,7 @@ Bug fixes + maintenance:
* Fix auto-delete if some icons do not exist anymore (#506) * Fix auto-delete if some icons do not exist anymore (#506)
* Fix notification icon color (#480, thanks to @s-h-a-r-d for reporting) * Fix notification icon color (#480, thanks to @s-h-a-r-d for reporting)
* Fix topics do not re-subscribe to Firebase after restoring from backup (#511) * Fix topics do not re-subscribe to Firebase after restoring from backup (#511)
* Fix crashes from large images (#474, thanks to @daedric7 for reporting)
* Add donate button (no ticket) * Add donate button (no ticket)
Additional translations: Additional translations: