Skip to content

fix(android): Read the SAF file size as a long - #25699

Merged
MartinZikmund merged 1 commit into
unoplatform:masterfrom
mikernet:dev/mikernet/android-saf-file-size
Nov 5, 2026
Merged

MartinZikmund merged 1 commit into
unoplatform:masterfrom
mikernet:dev/mikernet/android-saf-file-size

Conversation

@mikernet

Copy link
Copy Markdown
Contributor

GitHub Issue: closes #25698

PR Type:

🐞 Bugfix

What changed? 🚀

On Android, GetBasicPropertiesAsync for Storage Access Framework files (the files the pickers return) read the provider's _size column with Cursor.GetInt, into an int. A file of 2 GiB or more got a wrong Size: the high bits were lost, and a value that came out negative was sign-extended into a huge ulong.

SafHelpers.GetBasicPropertiesAsync now reads _size with GetLong, as DocumentsContract.Document.COLUMN_SIZE is a long. It checks IsNull first: a provider that doesn't know the size reports null ("Size of a document, in bytes, or null if unknown"), and Size is then 0. BasicProperties.Size is a ulong with no "unknown" value. 0 is also what AndroidX DocumentFile.length() returns "if the length is unknown", and Uno already uses that method when the provider returns no cursor. With the cursors I tried, a null _size already gave 0. Android documents the result of getInt and getLong on a null value as implementation-defined, though, so the code no longer depends on it.

_size the provider reports Before After
1,234 1,234 1,234
3 GiB (3,221,225,472) 18,446,744,072,635,809,792 3,221,225,472
5 GiB (5,368,709,120) 1,073,741,824 5,368,709,120
null 0 0

Folders (where the size isn't read) and providers that return no cursor (which use DocumentFile.length(), already a long) are unchanged. No other Android storage code reads a size with GetInt.

Tests: this PR has no automated test. Uno.UI.RuntimeTests builds once for net10.0 and runs on Android against the Android Uno.WinRT, so a runtime test can't use Android types such as cursors or content URIs. The only public routes to a SAF file are the pickers, which need a user. A test would need a content provider declared in the SamplesApp manifest that reports large and null sizes, plus an Android-only test entry point into Uno.WinRT. I can add that if you want it.

I verified the change on an Android 12 (API 31) emulator instead, with a throwaway app. It declares a content provider in a separate process, so the cursor crosses processes through a CursorWindow, as it does from a real documents provider. The provider reports a _size of 1,234, 3 GiB, 5 GiB or null. The app calls SafHelpers.GetBasicPropertiesAsync from Uno.WinRT built before and after this change. The "Before" and "After" columns above are its output.

PR Checklist ✅

Copilot AI balanced review requested due to automatic review settings October 25, 2026 20:09
@github-actions github-actions Bot added the platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform label Oct 25, 2026

Copilot AI left a comment

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.

🟢 Approval recommended

The narrow change corrects size truncation and handles unknown sizes without introducing identified regressions.

No open issues 🎉

What changed in this PR

Fixes Android SAF file-size reporting for files of 2 GiB or more, addressing #25698 in Uno’s storage implementation.

Changes:

  • Reads document sizes with GetLong into a long, avoiding 32-bit truncation.
  • Explicitly returns zero for unknown sizes while preserving folder and fallback behavior.
File Description
src/​Uno.WinRT/​Storage/​Internal/​SafHelpers.Android.cs Uses 64-bit size reads and checks for null provider values.

Review effort: Balanced

@MartinZikmund

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@unodevops

Copy link
Copy Markdown
Contributor

⚠️⚠️ The build 241293 has failed on Uno.UI - CI.

@MartinZikmund
MartinZikmund merged commit 3fc68f1 into unoplatform:master Nov 5, 2026
38 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] GetBasicPropertiesAsync reports a wrong Size for picked files of 2 GiB or more

4 participants