Skip to content

feat: add fileSize property to AssetEntity#1370

Open
dev-ale wants to merge 1 commit intofluttercandies:mainfrom
dev-ale:feature/asset-file-size
Open

feat: add fileSize property to AssetEntity#1370
dev-ale wants to merge 1 commit intofluttercandies:mainfrom
dev-ale:feature/asset-file-size

Conversation

@dev-ale
Copy link
Copy Markdown

@dev-ale dev-ale commented Feb 23, 2026

Summary

Adds a fileSize property to AssetEntity that reads the file size in bytes from platform metadata at query time, without downloading files from iCloud or other cloud storage.

Motivation

Currently, the only way to get an asset's file size is via asset.file or asset.originFile, which triggers full file downloads on iOS (including from iCloud). This is prohibitively slow for apps that need to display or calculate file sizes for many assets — for example, photo declutter apps showing "freed space" estimates.

Changes

Layer File Change
Dart lib/src/types/entity.dart Add fileSize field (nullable int?) to AssetEntity constructor, field declaration, and copyWith
Dart lib/src/utils/convert_utils.dart Read data['fileSize'] in convertMapToAsset()
iOS/macOS darwin/.../PMAssetPathEntity.h Add fileSize property to PMAssetEntity
iOS/macOS darwin/.../PMManager.m Populate fileSize from PHAssetResource metadata via value(forKey: "fileSize")
iOS/macOS darwin/.../PMConvertUtils.m Serialize fileSize in both convertPMAssetToMap: and convertPHAssetToMap:
Android core/entity/AssetEntity.kt Add fileSize: Long field
Android core/utils/IDBUtils.kt Add MediaStore.MediaColumns.SIZE to storeImageKeys and storeVideoKeys
Android extension/CursorExtensions.kt Read SIZE column from cursor, pass to entity
Android core/utils/ConvertUtils.kt Serialize fileSize to map
OHOS PhotoAssetHandler.ets Read SIZE (already in fetchColumns) and add ['fileSize', file_size] to map
Tests test/convert_utils_test.dart New test file: ConvertUtils.convertMapToAsset fileSize parsing + AssetEntity.copyWith preservation
Docs CHANGELOG.md Add entry under Unreleased

iOS implementation note

On iOS/macOS, PHAssetResource.value(forKey: "fileSize") is used to read file size from the Photos database metadata. This is a well-known KVC technique that reads locally cached metadata — it does not trigger iCloud downloads. While the fileSize key is not part of Apple's documented public API for PHAssetResource, it has been stable since the Photos framework was introduced and is widely used in the iOS ecosystem.

The implementation selects the primary resource (.photo, .video, .fullSizePhoto, or .fullSizeVideo) to report the most relevant file size, falling back to the first available resource.

Usage

final AssetEntity asset = ...;

// File size in bytes — available immediately, no download needed
final int? bytes = asset.fileSize;
if (bytes != null) {
  print('File size: ${(bytes / 1024 / 1024).toStringAsFixed(1)} MB');
}

Testing

  • dart analyze lib/ test/ — no issues
  • flutter test — all 15 tests pass (6 new tests added)
  • Manual testing on iOS device with iCloud photos
  • Manual testing on Android device

Closes #1369
Addresses #1366

Read file size from platform metadata at query time, without
downloading files from iCloud or other cloud storage.

- iOS/macOS: PHAssetResource.value(forKey: "fileSize")
- Android: MediaStore.MediaColumns.SIZE
- OHOS: photoAccessHelper.PhotoKeys.SIZE

The property is nullable (Int?) and only set when the platform
reports a value > 0.

Closes fluttercandies#1366
DATE_ADDED, // 创建时间
DATE_MODIFIED, // 修改时间
MIME_TYPE, // mime type
MediaStore.MediaColumns.SIZE, // file size in bytes
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Follow how other fields are being imported.

@property(nonatomic, assign) NSUInteger subtype;
@property(nonatomic, assign) BOOL favorite;
@property(nonatomic, assign) BOOL isLocallyAvailable;
@property(nonatomic, assign) long long fileSize;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems to be duplicate long

Comment thread lib/src/types/entity.dart
Comment on lines +903 to +904
/// This value is read from metadata at query time, without downloading
/// the file from iCloud or other cloud storage.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we clarify which file (compare to .file or .originalFile) the size indicated here refers to?

// let date_taken = this.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.DATE_TAKEN);
let orientation = PhotoAssetHandler.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.ORIENTATION);
let is_favorite = PhotoAssetHandler.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.FAVORITE);
let file_size = PhotoAssetHandler.getAssetMember(photoAsset, photoAccessHelper.PhotoKeys.SIZE);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you get any doc reference on this key?

@hitendigi
Copy link
Copy Markdown

I have tested this on both Android and iOS device and its working as expected, before this I had to run a separate process to fetch file size after getting image ids from the library. Using the same method that this PR used. Request to merge in stable release.

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.

[Feature request] Expose file size on AssetEntity without downloading

3 participants