Disclosure: I'm the developer of QuickDial, and this post is published from my account. It was drafted and published by an AI agent working on my behalf from the app's source code. QuickDial is an independent app, not affiliated with WhatsApp LLC or Meta.
QuickDial has a Share all (N) button: select several photos in a chat, tap it, and they go together, in chat order, to any app you pick. Share as PDF turns the same selection into one PDF with each photo's caption under it.
To share a photo, the app needs its file. The chat app saves received media in a folder the person grants once through the system folder picker (Storage Access Framework, so no storage permission and no "All files access"). The obvious code is: list the folder, filter by date. On one real phone that folder held 125,012 files, and a picker listing of it never finished. The user saw a spinner that said "searching 7 days" and nothing else.
This post is about the replacement: never list the big folder. Look files up by name, and use the fact that the names carry a counter that rises in save order.
The naming scheme is an index
Files in that folder are named like IMG-20261017-WA0167.jpg: a type, the day, and a 4-digit number. Measured on a phone, there is one counter per day, shared by photos, videos, received and sent files (voice notes and documents use numbers from it too). Sent files live in a Sent sub-folder.
That turns "list 125,000 entries" into "ask whether IMG-20261017-WA0042.jpg exists". With SAF you can do that without listing anything: build the document URI under the granted tree and query it.
private fun lookup(resolver: ContentResolver, tree: Uri, docId: String): MediaItem? =
runCatching {
resolver.query(DocumentsContract.buildDocumentUriUsingTree(tree, docId), PROJECTION, null, null, null)
?.use { c -> if (c.moveToFirst()) MediaItem(docId, c.getString(0), c.getLong(1), c.getLong(2)) else null }
}.getOrNull()
A missing document just throws or returns an empty cursor, so runCatching turns "not there" into null.
Where can number N be?
One number can be a received photo, a sent photo, a video, or the same in a less common format. The places are tried most common first, so a typical hit costs one or two queries:
private fun places() = listOf(
Place(imagesDir, "IMG", listOf("jpg")),
Place("$imagesDir/Sent", "IMG", listOf("jpg")),
Place(videoDir, "VID", listOf("mp4")),
Place("$videoDir/Sent", "VID", listOf("mp4")),
Place(imagesDir, "IMG", listOf("jpeg", "png")),
Place("$imagesDir/Sent", "IMG", listOf("jpeg", "png")),
)
The .jpeg and .png entries look like over-engineering until you find a real phone holding both. Skipping a real file matters more here than one extra query: the next step matches thumbnails to files, and a missing file can make a look-alike seem like the only candidate.
Reading a whole day
The simple version reads numbers 0, 1, 2, ... in batches of 32, twelve lookups in parallel, and stops after 60 numbers in a row that exist nowhere. Gaps are normal (deleted photos, voice notes, documents), so one miss means nothing; 60 in a row means the day is done.
One rule is deliberately strict: there is a time budget, and when it runs out the day is given up, not returned in part. A day read in part could leave out the true file and make a wrong one look unique. "I couldn't search that day" is a better answer than a confident wrong share.
Reading only a time range: gallop, then bisect
Reading every number of a busy day is still slow. Usually we know roughly when the photos were saved (from the chat bubbles), so we only need numbers saved between fromMs and toMs.
Because the counter runs in save order, save time rises with the number. That makes it searchable:
-
Gallop: test 16, 32, 64, 128, ... until a number is not saved before
fromMs. - Bisect between the last "before" and that point, down to a gap of 16.
-
Read forward from there until two files in a row are past
toMs, or 60 numbers in a row are missing.
The interesting part is the test "is number N saved before fromMs?", because N may not exist:
fun before(start: Int): Boolean {
var at = start
repeat(GAP_WINDOWS) { // 5 windows of 8 numbers
val hits = lookup((at until at + WINDOW).toList()).filterNotNull()
if (hits.isNotEmpty()) return hits.maxOf { it.modifiedAt } < fromMs
at += WINDOW
}
return false // nothing found: NOT "before"
}
An empty stretch answers false. That choice is the safety property: a gap can only make the scan start earlier, never later. The start moves forward only on evidence (real files, all saved before the range). Starting early costs a few extra lookups; starting late silently skips the photo the user selected.
A cheaper probe helps the bisect: finding where to start needs only some files, so it looks in the received-photos place alone (one query per number). If that window is empty, it is checked in every place before it counts as a gap, because a day can have no received photos at all and only sent ones.
The re-save trap
One real album stopped short: numbers 0167 to 0170 of 17 Oct had been saved again on 18 Oct, so the forward read saw "past the range" twice and stopped before 0173 to 0176, which were the album.
A file whose save day is later than the day in its name is a re-save. It says nothing about where the day's counter is, so it is kept as a candidate but ignored by the stop rule:
val resaved = item.nameDay?.let { dayOf(item.modifiedAt) > it } == true
if (!resaved) { if (item.modifiedAt > toMs) { if (++past >= 2) break } else past = 0 }
out += item
Small folders are still just listed
Not everything gets the lookup treatment. A sub-folder used for chats with media visibility turned off held about 4,900 files, and in one case a file there kept an old name from months earlier, so no lookup by name could find it. That folder is listed whole, but only below a size cap (about 30,000 entries, judged from the directory's own size before reading), cached for 10 minutes while unchanged, and read ahead in the background as soon as a selection starts. Above the cap it falls back to lookups by name.
Takeaways
- If file names carry a counter in save order, they are an index. Query by name; don't list.
- Order the places by how common they are, but don't drop the rare ones if a miss can make a wrong match look unique.
- Gallop and bisect on save time to jump to a range; make every "don't know" push the start earlier, never later.
- A partial result that looks complete is worse than an honest "gave up". Fail the whole unit.
- Watch for items whose metadata disagrees with their name (re-saves) and keep them out of your stop rules.
QuickDial is on Google Play. Independent app, not affiliated with WhatsApp LLC or Meta.
AI Disclosure: this article was drafted and published by an AI agent on the developer's behalf, from the app's source code.
Top comments (0)