asset_info.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import 'dart:io';
  2. import 'dart:typed_data';
  3. import 'package:wechat_assets_picker/wechat_assets_picker.dart';
  4. class AssetInfo {
  5. final String id;
  6. final int width;
  7. final int height;
  8. final int? duration;
  9. final int orientation;
  10. final int typeInt;
  11. final int? createDateSecond;
  12. final DateTime createDateTime;
  13. final DateTime modifiedDateTime;
  14. final String? title;
  15. final String? relativePath;
  16. final String? localPath;
  17. final String? filePath;
  18. final String? thumbFilePath;
  19. int? size;
  20. AssetInfo({
  21. required this.id,
  22. required this.width,
  23. required this.height,
  24. this.duration,
  25. required this.orientation,
  26. required this.typeInt,
  27. this.createDateSecond,
  28. required this.createDateTime,
  29. required this.modifiedDateTime,
  30. this.localPath,
  31. this.title,
  32. this.relativePath,
  33. this.filePath,
  34. this.thumbFilePath,
  35. this.size,
  36. });
  37. // 从 AssetEntity 创建
  38. factory AssetInfo.fromAssetEntity(
  39. AssetEntity entity, String filePath, String thumbFilePath) {
  40. return AssetInfo(
  41. id: entity.id,
  42. width: entity.width,
  43. height: entity.height,
  44. duration: entity.duration,
  45. orientation: entity.orientation,
  46. typeInt: entity.typeInt,
  47. createDateSecond: entity.createDateSecond,
  48. createDateTime: entity.createDateTime,
  49. modifiedDateTime: entity.modifiedDateTime,
  50. title: entity.title,
  51. relativePath: entity.relativePath,
  52. filePath: filePath,
  53. thumbFilePath: thumbFilePath,
  54. );
  55. }
  56. // 转换为 JSON
  57. Map<String, dynamic> toJson() => {
  58. 'id': id,
  59. 'width': width,
  60. 'height': height,
  61. 'duration': duration,
  62. 'orientation': orientation,
  63. 'typeInt': typeInt,
  64. 'createDateSecond': createDateSecond,
  65. 'createDateTime': createDateTime.toIso8601String(),
  66. 'modifiedDateTime': modifiedDateTime.toIso8601String(),
  67. 'title': title,
  68. 'relativePath': relativePath,
  69. 'localPath': localPath,
  70. 'file': filePath,
  71. 'thumbFile': thumbFilePath,
  72. };
  73. // 从 JSON 创建
  74. factory AssetInfo.fromJson(Map<String, dynamic> json) => AssetInfo(
  75. id: json['id'] as String,
  76. width: json['width'] as int,
  77. height: json['height'] as int,
  78. duration: json['duration'] as int,
  79. orientation: json['orientation'] as int,
  80. // isAll: json['isAll'] as bool,
  81. typeInt: json['typeInt'] as int,
  82. createDateSecond: json['createDateSecond'] as int?,
  83. createDateTime: DateTime.parse(json['createDateTime'] as String),
  84. modifiedDateTime: DateTime.parse(json['modifiedDateTime'] as String),
  85. title: json['title'] as String?,
  86. relativePath: json['relativePath'] as String?,
  87. localPath: json['localPath'] as String?,
  88. filePath: json['filePath'] as String?,
  89. thumbFilePath: json['thumbFilePath'] as String?,
  90. );
  91. // MARK: - To Do
  92. // 转换为 AssetEntity
  93. Future<AssetEntity?> toAssetEntity() async {
  94. return await AssetEntity.fromId(id);
  95. }
  96. // 获取文件对象
  97. File get file => File(filePath!);
  98. // 获取缩略图数据
  99. Future<Uint8List?> get thumbnailData async {
  100. File thumbFile = File(thumbFilePath!);
  101. try {
  102. if (await thumbFile.exists()) {
  103. return await thumbFile.readAsBytes();
  104. }
  105. return null;
  106. } catch (e) {
  107. print('获取缩略图失败: $e');
  108. return null;
  109. }
  110. }
  111. }