-
Notifications
You must be signed in to change notification settings - Fork 383
Description
Use case
When building photo management or declutter apps, it's common to display file sizes and calculate freed storage. Currently, the only way to get file size through photo_manager is by downloading the file first:
final file = await asset.file; // triggers iCloud download on iOS
final bytes = await file?.length() ?? 0;This is problematic because:
- iCloud photos: Triggers a full download just to read the size, which is slow and uses bandwidth/storage
- Performance: Reading thousands of file sizes serially via
asset.fileis too slow for batch operations - Storage impact: On iOS,
asset.filecan rapidly fill the device cache
Proposed solution
Add a fileSize property to AssetEntity that is populated at query time from platform metadata:
| Platform | Source | Notes |
|---|---|---|
| iOS/macOS | `PHAssetResource.value(forKey: "fileSize")` | Reads from Photos database metadata — no iCloud download required |
| Android | `MediaStore.MediaColumns.SIZE` (`_size` column) | Already available in MediaStore, just not queried |
| OHOS | `photoAccessHelper.PhotoKeys.SIZE` | Already fetched in `fetchColumns`, just not serialized |
API
final AssetEntity asset = ...;
final int? fileSize = asset.fileSize; // bytes, or null if unavailableNotes on the iOS implementation
The fileSize key on PHAssetResource is accessed via KVC (value(forKey:)). While not part of Apple's public API documentation for PHAssetResource, it is a well-established technique used widely in the iOS ecosystem (e.g., TLPhotoPicker, various StackOverflow answers). The property has been stable across all iOS versions since Photos framework was introduced.
The public properties on PHAssetResource are: assetLocalIdentifier, originalFilename, type, and uniformTypeIdentifier. Apple does not currently provide a public API for reading file size without downloading the resource data.
Related issues
- [Feature request] Expose asset size property without downloading file #1366 — Feature request for exposing asset size without downloading
- [Feature request] Expose exact file size from iOS Photos app info #1360 — Request for exact file size from iOS Photos info
- [Bug report] file.length() returns incorrect image size on iOS #1359 — Bug report about
file.length()returning incorrect sizes
Implementation
I have a working implementation ready as a PR that covers all three platforms with tests. See the linked PR.