asset_info.dart 3.6 KB

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