Skip to content

Resolve file URIs through ContentResolver so Storage putFile(Uri) works - #84

Merged
nbransby merged 3 commits into
masterfrom
content-resolver-files
Nov 9, 2026
Merged

nbransby merged 3 commits into
masterfrom
content-resolver-files

Conversation

@nbransby

@nbransby nbransby commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Firebase Storage's putFile(Uri) opens the file through ContentResolver.openFileDescriptor to learn its size, reads it through openInputStream, and asks getType for the content type when the metadata has none. The android.content.ContentResolver shim was an empty class, so on the JVM putFile(Uri) failed with NoSuchMethodError: ContentResolver.openFileDescriptor.

ContentResolver now resolves file: URIs (and bare paths) to files: openFileDescriptor, openInputStream, openOutputStream (with the w/wt/wa/rw/rwt modes) and getType, which guesses the MIME type from the file name. Other schemes are reported as FileNotFoundException, since there are no content providers on the JVM. A new android.os.ParcelFileDescriptor shim backs it, holding a RandomAccessFile with the Android MODE_* flags and parseMode.

ContentResolverTest covers the size reported through the descriptor for file:/, file:/// and bare-path URIs, reading and writing streams, append and truncate modes, the type guess, and the not-found cases.

Verified end to end against the Storage emulator with the firebase-kotlin-sdk storage tests on the JVM: with this jar, putFile(Uri) of a downloaded file uploads it with the right size.

Firebase Storage's putFile(Uri) opens the file through
ContentResolver.openFileDescriptor to learn its size and then reads it
through openInputStream, and uses getType for the content type when the
metadata has none. The ContentResolver shim was empty, so putFile(Uri)
failed on the JVM with a NoSuchMethodError.

ContentResolver now resolves file: URIs (and bare paths) to files, backed
by a new ParcelFileDescriptor shim over a RandomAccessFile with the
Android mode flags, and guesses the MIME type from the file name. Other
schemes are reported as not found, as there are no content providers on
the JVM.
Copilot AI balanced review requested due to automatic review settings November 8, 2026 13:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved issues include a missing adoptFd API that breaks Java compilation and file-mode handling that can truncate data, violate access restrictions, or overwrite interleaved append writes.

4 open issues
What changed in this PR

This PR adds JVM file access to the Android ContentResolver shim so Firebase Storage’s putFile(Uri) can read file sizes, contents, and inferred MIME types.

Changes:

  • Replaces the empty Java resolver with a Kotlin implementation supporting file URIs and bare paths.
  • Adds a ParcelFileDescriptor shim with file-size reporting and Android mode flags.
  • Adds tests for URI formats, streams, write modes, MIME types, and missing files.
File Description
src/​test/​kotlin/​ContentResolverTest.kt Adds coverage for resolver and descriptor behavior.
src/​main/​java/​android/​os/​ParcelFileDescriptor.kt Adds file-backed descriptors and mode parsing.
src/​main/​java/​android/​content/​ContentResolver.kt Implements file resolution, stream access, and MIME inference.
src/​main/​java/​android/​content/​ContentResolver.java Removes the empty shim replaced by Kotlin.

Review effort: Balanced

Read-only descriptors are backed by a FileInputStream, write-only ones by
a FileOutputStream that truncates or appends (so concurrent "wa"
descriptors interleave at the end of the file), and read-write ones by a
RandomAccessFile that no longer truncates for "rw". openOutputStream
rejects read-only modes and writes in place for "rw". adoptFd is declared
and unsupported, matching the SQLite shim's reference.
@nbransby
nbransby merged commit cb658e2 into master Nov 9, 2026
1 check passed
@nbransby
nbransby deleted the content-resolver-files branch November 9, 2026 05:33
nbransby added a commit to GitLiveApp/firebase-kotlin-sdk that referenced this pull request Nov 9, 2026
firebase-java-sdk 0.8.1 implements `ContentResolver.openFileDescriptor` and the file-backed `ContentResolver` streams (GitLiveApp/firebase-java-sdk#84), so Firebase Storage's `putFile(Uri)` now works on the JVM. The JVM `supportsFileUploads` test gate added in #889 for that gap is removed, so the storage upload test runs on the JVM as well.

All 25 storage JVM tests pass locally against the published 0.8.1 artifact and the Storage emulator, the upload test included.
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.

2 participants