Repository navigation
Description
Current behavior 🐛
On Android, files from FileOpenPicker, FileSavePicker and FolderPicker are Storage Access Framework (SAF) documents. For them, GetBasicPropertiesAsync reads the provider's _size column (DocumentsContract.Document.COLUMN_SIZE, "Type: INTEGER (long)") with Cursor.GetInt, into an int, in src/Uno.WinRT/Storage/Internal/SafHelpers.Android.cs. For a file of 2 GiB or more, BasicProperties.Size is wrong:
| Size the provider reports | BasicProperties.Size |
|---|---|
| 1,234 | 1,234 |
| 3 GiB (3,221,225,472) | 18,446,744,072,635,809,792 (the int is -1 GiB, then sign-extended to ulong) |
| 5 GiB (5,368,709,120) | 1,073,741,824 (the high bits are lost) |
Android documents what getInt returns for a value outside the int range, or for a null value, as implementation-defined. A provider reports a null _size when it doesn't know the size ("Size of a document, in bytes, or null if unknown"). Uno then gets 0 from the cursors I tried, but only by that implementation-defined behavior.
Expected behavior 🎯
Size is the size the provider reports, read as a long: 3,221,225,472 and 5,368,709,120 above.
When the provider doesn't know the size, Size is 0. BasicProperties.Size is a ulong with no "unknown" value, and 0 is what AndroidX DocumentFile.length() returns "if the length is unknown". Uno already uses DocumentFile.length() when the provider returns no cursor.
How to reproduce it (as minimally and precisely as possible) 🔬
On Android, pick a file of 3 GiB or more (a long video, for example) and read its size:
var picker = new FileOpenPicker();
picker.FileTypeFilter.Add("*");
var file = await picker.PickSingleFileAsync();
var properties = await file.GetBasicPropertiesAsync();
// properties.Size is not the file's sizeI reproduced it on an Android 12 (API 31) emulator, without a picker: a test app with a content provider in a separate process, which reports a _size of 3 GiB, 5 GiB or null, calls Uno's SafHelpers.GetBasicPropertiesAsync for its URIs. The sizes are the ones in the table above.
Workaround 🛠️
None through Uno's storage APIs. An app can query the document URI with Android's ContentResolver itself and read _size with GetLong.
Affected platforms 📱💻🖥️
Android
Uno.Sdk version (and other relevant versions) 📦
Seen in Uno.Sdk 6.8. The code is the same on master: it has read _size with GetInt since SAF storage was added in 2021.
IDE version 🧑💻
Not IDE-related.
Anything else we need to know? 💬
I have a fix ready and will open a PR.
Activity