files: downsample thumbnails to fix oversized-bitmap crash (#768)
- Commit
cd615a8ff3a3408da08a11c23fecda9352923e22- Author
- vayun-mathur <[email protected]>
- When
- 2026-09-30 00:20 UTC (2 months ago)
- Parents
51887e6
Diff
files/src/main/java/com/vayunmathur/files/ui/DirectoryLeading.kt
diff --git a/files/src/main/java/com/vayunmathur/files/ui/DirectoryLeading.kt b/files/src/main/java/com/vayunmathur/files/ui/DirectoryLeading.ktindex ac2c8f80d6..c04397480c 100644--- a/files/src/main/java/com/vayunmathur/files/ui/DirectoryLeading.kt+++ b/files/src/main/java/com/vayunmathur/files/ui/DirectoryLeading.kt@@ -7,9 +7,11 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale+import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import com.vayunmathur.files.platform.FileBrowserItem+import com.vayunmathur.library.image.ImageRequest import com.vayunmathur.library.image.compose.AsyncImage import com.vayunmathur.library.ui.IconArchive import com.vayunmathur.library.ui.IconCode@@ -39,13 +41,27 @@ internal val COLOR_ARCHIVE = Color(0xFFB28500) internal val COLOR_APK = Color(0xFF009688) internal val COLOR_CODE = Color(0xFF607D8B) +/**+ * Pixel edge requested for file-browser thumbnails. The tiles paint at 40-56 dp,+ * so 256 px stays sharp on xxxhdpi and matches the Photos grid size.+ */+internal const val THUMBNAIL_SIZE_PX = 256+ /** Leading visual for a browser item: an image thumbnail, or a type-colored icon. */ @Composable internal fun FileLeading(item: FileBrowserItem, isSelected: Boolean, sizeDp: Dp) {+ val context = LocalContext.current val ext = item.name.substringAfterLast('.', "").lowercase() if (!item.isDirectory && item.realFile != null && ext in IMAGE_EXTS) { AsyncImage(- model = item.realFile,+ // Explicit thumbnail size: decoding a 40-56 dp tile at full+ // resolution produced ~500 MB bitmaps that crashed Canvas on draw+ // (files #768). 256 px matches the Photos grid and also keys the+ // cache per size instead of colliding with full-res entries.+ model = ImageRequest.Builder(context)+ .data(item.realFile)+ .size(THUMBNAIL_SIZE_PX)+ .build(), contentDescription = null, modifier = Modifier .size(sizeDp)
library/image/src/main/java/com/vayunmathur/library/image/decoders/BitmapDecoder.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/decoders/BitmapDecoder.kt b/library/image/src/main/java/com/vayunmathur/library/image/decoders/BitmapDecoder.ktindex 48da33b519..1f34f53280 100644--- a/library/image/src/main/java/com/vayunmathur/library/image/decoders/BitmapDecoder.kt+++ b/library/image/src/main/java/com/vayunmathur/library/image/decoders/BitmapDecoder.kt@@ -9,6 +9,17 @@ import java.nio.ByteBuffer object BitmapDecoder { + /**+ * Longest edge an unconstrained ("original size") decode may produce.+ *+ * Requests without `.size(...)` used to decode at full resolution, so one+ * panorama or high-MP photo became a ~500 MB bitmap that crashed in+ * `RecordingCanvas.throwIfCannotDraw` as soon as it was drawn (files #768).+ * 4096 keeps full-screen viewers sharp while a square 4096x4096 ARGB_8888+ * bitmap is 64 MB, comfortably under Canvas's draw limit.+ */+ const val MAX_ORIGINAL_DIMENSION = 4096+ fun isSvg(bytes: ByteArray): Boolean { if (bytes.size < 5) return false val head = String(bytes.take(1024).toByteArray()).trimStart()@@ -57,6 +68,17 @@ object BitmapDecoder { if (ratio > 1f) { decoder.setTargetSize((w / ratio).toInt().coerceAtLeast(1), (h / ratio).toInt().coerceAtLeast(1)) }+ } else if (targetW <= 0 || targetH <= 0) {+ // Unconstrained request: never hand back a bitmap larger than+ // MAX_ORIGINAL_DIMENSION on its longest edge (see files #768).+ val longest = maxOf(w, h)+ if (longest > MAX_ORIGINAL_DIMENSION) {+ val ratio = longest.toFloat() / MAX_ORIGINAL_DIMENSION+ decoder.setTargetSize(+ (w / ratio).toInt().coerceAtLeast(1),+ (h / ratio).toInt().coerceAtLeast(1),+ )+ } } decoder.isUnpremultipliedRequired = false decoder.allocator = if (allowHardware) {@@ -70,6 +92,30 @@ object BitmapDecoder { } } + /**+ * Power-of-two downsampling for the `BitmapFactory` fallback, pure arithmetic+ * so it is unit-testable without Android bitmaps.+ *+ * A sized request samples to roughly the target; an unconstrained request+ * ("original size") still caps the longest edge at [MAX_ORIGINAL_DIMENSION]+ * so no caller can produce a Canvas-crashing bitmap (files #768).+ */+ internal fun sampleSizeFor(srcW: Int, srcH: Int, reqW: Int, reqH: Int): Int {+ var sample = 1+ if (reqW > 0 && reqH > 0 && srcW > 0 && srcH > 0) {+ val halfW = srcW / 2+ val halfH = srcH / 2+ while (halfW / sample >= reqW && halfH / sample >= reqH) {+ sample *= 2+ }+ } else if (srcW > 0 && srcH > 0 && (srcW > MAX_ORIGINAL_DIMENSION || srcH > MAX_ORIGINAL_DIMENSION)) {+ while (srcW / sample > MAX_ORIGINAL_DIMENSION || srcH / sample > MAX_ORIGINAL_DIMENSION) {+ sample *= 2+ }+ }+ return sample.coerceAtLeast(1)+ }+ private fun decodeWithBitmapFactory( bytes: ByteArray, reqW: Int,@@ -80,14 +126,7 @@ object BitmapDecoder { BitmapFactory.decodeByteArray(bytes, 0, bytes.size, boundsOpts) val (w, h) = boundsOpts.outWidth to boundsOpts.outHeight - var sample = 1- if (reqW > 0 && reqH > 0 && w > 0 && h > 0) {- var halfW = w / 2- var halfH = h / 2- while (halfW / sample >= reqW && halfH / sample >= reqH) {- sample *= 2- }- }+ val sample = sampleSizeFor(w, h, reqW, reqH) val opts = BitmapFactory.Options().apply { inSampleSize = sample.coerceAtLeast(1)
library/image/src/test/java/com/vayunmathur/library/image/decoders/BitmapDecoderTest.kt
diff --git a/library/image/src/test/java/com/vayunmathur/library/image/decoders/BitmapDecoderTest.kt b/library/image/src/test/java/com/vayunmathur/library/image/decoders/BitmapDecoderTest.ktnew file mode 100644index 0000000000..f187ec0b1d--- /dev/null+++ b/library/image/src/test/java/com/vayunmathur/library/image/decoders/BitmapDecoderTest.kt@@ -0,0 +1,35 @@+package com.vayunmathur.library.image.decoders++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class BitmapDecoderTest {++ @Test+ fun sizedThumbnailRequestSamplesDown() {+ // 4000x3000 photo into a 256px thumbnail: halves 2000/1500 -> 1000/750 -> 500/375,+ // next step would drop below the target.+ assertEquals(8, BitmapDecoder.sampleSizeFor(4000, 3000, 256, 256))+ }++ @Test+ fun unconstrainedHugePhotoIsCappedAtMaxDimension() {+ // Regression test for files #768: an "original size" decode of a huge photo+ // must not come back at full resolution (~500 MB bitmap -> Canvas crash).+ val sample = BitmapDecoder.sampleSizeFor(12000, 8000, -1, -1)+ assertTrue(sample > 1, "expected downsampling, got $sample")+ assertTrue(12000 / sample <= BitmapDecoder.MAX_ORIGINAL_DIMENSION)+ assertTrue(8000 / sample <= BitmapDecoder.MAX_ORIGINAL_DIMENSION)+ }++ @Test+ fun unconstrainedSmallPhotoIsUntouched() {+ assertEquals(1, BitmapDecoder.sampleSizeFor(1000, 800, -1, -1))+ }++ @Test+ fun sizedRequestSmallerThanSourceIsUntouched() {+ assertEquals(1, BitmapDecoder.sampleSizeFor(100, 100, 256, 256))+ }+}