Skip to content

fix: use unique filename per image in gallery fallback path#16

Merged
andredestro merged 2 commits intomainfrom
fix/gallery-duplicate-webpath
Apr 28, 2026
Merged

fix: use unique filename per image in gallery fallback path#16
andredestro merged 2 commits intomainfrom
fix/gallery-duplicate-webpath

Conversation

@andredestro
Copy link
Copy Markdown
Contributor

Problem

When selecting multiple images from the gallery on certain Android devices (confirmed: Samsung Galaxy S10 / Android 12 / One UI 4.2), all results returned the same uri and webPath:
/data/user/0/com.app/cache/file.jpg

This happened because getRealPath() returns null for photo picker URIs on devices where the _data MediaStore column is unavailable (deprecated in Android 10+ scoped storage). The fallback downloadCacheFileFromInputStream was then used, writing every selected image to the same hardcoded filename "file.<extension>" — each image overwriting the previous one.

Fixes: ionic-team/capacitor-camera#47

Fix

Replace the hardcoded filename with a UUID-based unique name in downloadCacheFileFromInputStream, ensuring each selected image gets its own distinct cache file.

Copy link
Copy Markdown
Contributor

@OS-pedrogustavobilro OS-pedrogustavobilro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a remark, but may not need changes in your PR (unless you disagree). Approved 🤌

val buffer = ByteArray(8 * 1024)
val extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) ?: "dat"
val targetFile = createCaptureFile(context, "file.${extension}")
val targetFile = createCaptureFile(context, "${UUID.randomUUID()}.${extension}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this seems to fix the issue, it brings up the situation of the cache size growing with the amount of files being returned from the gallery. But that also applies to the photos taken with camera.

Unlike iOS, the cached images are not cleared on app restart, only the videos.

That's possibly a discrepancy we should address one day, but guess this PR may not be the place for it (unless you disagree).

@andredestro andredestro merged commit 9865044 into main Apr 28, 2026
1 check passed
@andredestro andredestro deleted the fix/gallery-duplicate-webpath branch April 28, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Multiple Selection returns the same WebPath for every Image

2 participants