Skip to content

[Feature request] Expose file size on AssetEntity without downloading #1369

@dev-ale

Description

@dev-ale

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:

  1. iCloud photos: Triggers a full download just to read the size, which is slow and uses bandwidth/storage
  2. Performance: Reading thousands of file sizes serially via asset.file is too slow for batch operations
  3. Storage impact: On iOS, asset.file can 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 unavailable

Notes 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

Implementation

I have a working implementation ready as a PR that covers all three platforms with tests. See the linked PR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions