I have an Android application with camera and album features. It can take high-resolution photos and display them in albums. The problem is that each photo can be several hundred MB, which significantly increases the loading time when the user accesses the album page. I'm using Coil, and with RAM caching, the loading is very fast after the first attempt. However, the cache is cleared after a certain time, and it needs to be reloaded when the user opens the album page again. I tried using disk caching with Coil, but I think Coil doesn't offer disk caching for local images. What's the best way to do this?
I keep my files in this location:
/storage/emulated/0/CameraApp/Album/
Thank you
Update for those who might experience the same problem in the future
I'm trying to create a small thumbnail using a third-party library and embed it in the EXIF file. It seems to be working for now, but I'll conduct a more comprehensive test. the repository I'm using https://github.com/StefanOltmann/kim
You generally should not cache local images manually in LocalStorage / RAM if the files already exist on disk.
For Android image apps, the best practice is:
Store the original image on disk (which you already do)
Generate and cache thumbnails/previews, not the full-resolution image
Load scaled images into the UI using Coil/Glide
Right now the real problem is probably that you're loading the full-resolution images (several hundred MB) into the album grid/list. Even SSD storage becomes slow with files that large.
Best approach:
AsyncImage(
model = ImageRequest.Builder(context)
.data(file)
.size(300) // thumbnail size
.crossfade(true)
.build(),
contentDescription = null
)
or with Coil:
.allowHardware(true)
.memoryCachePolicy(CachePolicy.ENABLED)
.diskCachePolicy(CachePolicy.ENABLED)
Also:
Use thumbnails for album pages
Load original image only when user opens the photo
Consider generating thumbnails once and saving them in a .thumbnails folder
Coil does cache local images, but caching huge originals is inefficient and may be skipped depending on request size/configuration
If images are really “hundreds of MB”, you should also check:
image compression
bitmap sampling (inSampleSize)
HEIF/WebP/JPEG quality settings
Loading gigantic originals into RecyclerView/Grid is usually the main bottleneck.