Understand the plugin's caching mechanism
mainThe plugin uses a caching mechanism to handle file access across different platforms:
Android
- Android 10 (Q, 29): Direct access to resource paths is restricted. The plugin generates image caches during I/O operations (e.g., when calling
.fileor.originFile). You can userequestLegacyExternalStorageto access files without caching. - Android 11+: Direct access to resource paths is available again.
iOS
- iOS does not provide an API to access original files directly. When calling
.fileor.originFile, the plugin generates a cache file within the application's sandbox. - Important: If disk space is a concern, you should manually delete these cache files after use.
Clearing Cache
You can clear all plugin-generated caches using PhotoManager.clearFileCache.
import 'dart:io';
Future<void> useEntity(AssetEntity entity) async {
File? file;
try {
file = await entity.file;
await handleFile(file!); // 处理获取的文件
} finally {
if (Platform.isIOS) {
file?.deleteSync(); // 处理完成后删除
}
}
}