Image loading

Networking Junior Kotlin 2.0+ API 24+ Reviewed By Best Latest Apps

Predict the output

Roughly how much memory does a fully decoded 4000 × 3000 ARGB_8888 bitmap use?

fun main() {
    val bytes = 4_000L * 3_000L * 4L
    println("${bytes / 1_000_000} MB")
}
Options
Hint 1 of 1

How many bytes per pixel in ARGB_8888?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A 48 MB

Correct. Width × height × 4 bytes per pixel.

Why not the others?
  • B 3 MB, about the JPEG size. The file is compressed; decoded pixels aren't.
  • C 12 MB. Each ARGB_8888 pixel takes 4 bytes, not 1.
  • D 480 MB. Off by a factor of ten.

30-second answer

It downloads, decodes and caches images in memory and on disk, downsamples them to the size of the target view, reuses bitmaps, cancels loads when a row scrolls away, and handles placeholders and errors. You still need to request the right size, avoid loading full-resolution images into thumbnails, and give stable keys so lists don't reload images.

Practise saying it · 30 s. No recording, just a timer

Explain what image libraries do.

Say your answer out loud, then check which of these points you covered:

  • Download, decode, cache
  • Downsample to target size
  • Cancel off-screen loads
  • Bitmap memory = w × h × 4

Deep dive

Coil is Kotlin- and coroutine-first, with AsyncImage for Compose.

Decoding a 4000 × 3000 photo at full size takes about 48 MB; downsampling avoids out-of-memory errors.

Hardware bitmaps save memory but can't be read pixel by pixel.

Preload the next images in a feed for smooth scrolling.

AsyncImage in a list row Compile-checked · Kotlin 2.4.21
@Composable
fun Avatar(url: String) {
    AsyncImage(
        model = url,
        contentDescription = null,
        modifier = Modifier.size(48.dp),
    )
}

Common mistakes

  • Decoding images yourself with BitmapFactory in a list.
  • Loading original-size images into small thumbnails.
  • Using several image libraries in one app.

Senior take & follow-ups

Do the bitmap memory maths; it's what interviewers look for.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why does downsampling matter?

    Show hint

    Memory is width × height × 4 bytes regardless of file size.

  2. How does a list avoid loading images for scrolled-away rows?

    Show hint

    The library cancels the request when the view or composable leaves.

Suggest a fix if something here is wrong or out of date.