Browse Source

compress when pick image from gallery

master
daivph 5 years ago
parent
commit
2f49e5a282
6 changed files with 79 additions and 22 deletions
  1. +29
    -0
      ios/Podfile.lock
  2. +20
    -21
      lib/presentation/custom_widgets/widget_media_picker.dart
  3. +20
    -0
      lib/presentation/screens/actions/util_action.dart
  4. +2
    -1
      lib/utils/const_string.dart
  5. +7
    -0
      pubspec.lock
  6. +1
    -0
      pubspec.yaml

+ 29
- 0
ios/Podfile.lock View File

- Protobuf (>= 3.9.2, ~> 3.9) - Protobuf (>= 3.9.2, ~> 3.9)
- FLAnimatedImage (1.0.12) - FLAnimatedImage (1.0.12)
- Flutter (1.0.0) - Flutter (1.0.0)
- flutter_image_compress (0.0.1):
- Flutter
- Mantle
- SDWebImageWebPCoder
- FMDB (2.7.5): - FMDB (2.7.5):
- FMDB/standard (= 2.7.5) - FMDB/standard (= 2.7.5)
- FMDB/standard (2.7.5) - FMDB/standard (2.7.5)
- GoogleUtilities/Logger - GoogleUtilities/Logger
- GoogleUtilities/UserDefaults (6.7.2): - GoogleUtilities/UserDefaults (6.7.2):
- GoogleUtilities/Logger - GoogleUtilities/Logger
- libwebp (1.1.0):
- libwebp/demux (= 1.1.0)
- libwebp/mux (= 1.1.0)
- libwebp/webp (= 1.1.0)
- libwebp/demux (1.1.0):
- libwebp/webp
- libwebp/mux (1.1.0):
- libwebp/demux
- libwebp/webp (1.1.0)
- Mantle (2.1.4):
- Mantle/extobjc (= 2.1.4)
- Mantle/extobjc (2.1.4)
- MTBBarcodeScanner (5.0.11) - MTBBarcodeScanner (5.0.11)
- nanopb (1.30906.0): - nanopb (1.30906.0):
- nanopb/decode (= 1.30906.0) - nanopb/decode (= 1.30906.0)
- SDWebImageFLPlugin (0.4.0): - SDWebImageFLPlugin (0.4.0):
- FLAnimatedImage (>= 1.0.11) - FLAnimatedImage (>= 1.0.11)
- SDWebImage/Core (~> 5.6) - SDWebImage/Core (~> 5.6)
- SDWebImageWebPCoder (0.6.1):
- libwebp (~> 1.0)
- SDWebImage/Core (~> 5.7)
- shared_preferences (0.0.1): - shared_preferences (0.0.1):
- Flutter - Flutter
- sqflite (0.0.1): - sqflite (0.0.1):
- firebase_core (from `.symlinks/plugins/firebase_core/ios`) - firebase_core (from `.symlinks/plugins/firebase_core/ios`)
- firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`) - firebase_messaging (from `.symlinks/plugins/firebase_messaging/ios`)
- Flutter (from `Flutter`) - Flutter (from `Flutter`)
- flutter_image_compress (from `.symlinks/plugins/flutter_image_compress/ios`)
- package_info (from `.symlinks/plugins/package_info/ios`) - package_info (from `.symlinks/plugins/package_info/ios`)
- path_provider (from `.symlinks/plugins/path_provider/ios`) - path_provider (from `.symlinks/plugins/path_provider/ios`)
- shared_preferences (from `.symlinks/plugins/shared_preferences/ios`) - shared_preferences (from `.symlinks/plugins/shared_preferences/ios`)
- FMDB - FMDB
- GoogleDataTransport - GoogleDataTransport
- GoogleUtilities - GoogleUtilities
- libwebp
- Mantle
- MTBBarcodeScanner - MTBBarcodeScanner
- nanopb - nanopb
- PromisesObjC - PromisesObjC
- Protobuf - Protobuf
- SDWebImage - SDWebImage
- SDWebImageFLPlugin - SDWebImageFLPlugin
- SDWebImageWebPCoder
- SwiftProtobuf - SwiftProtobuf


EXTERNAL SOURCES: EXTERNAL SOURCES:
:path: ".symlinks/plugins/firebase_messaging/ios" :path: ".symlinks/plugins/firebase_messaging/ios"
Flutter: Flutter:
:path: Flutter :path: Flutter
flutter_image_compress:
:path: ".symlinks/plugins/flutter_image_compress/ios"
package_info: package_info:
:path: ".symlinks/plugins/package_info/ios" :path: ".symlinks/plugins/package_info/ios"
path_provider: path_provider:
FirebaseMessaging: 29543feb343b09546ab3aa04d008ee8595b43c44 FirebaseMessaging: 29543feb343b09546ab3aa04d008ee8595b43c44
FLAnimatedImage: 4a0b56255d9b05f18b6dd7ee06871be5d3b89e31 FLAnimatedImage: 4a0b56255d9b05f18b6dd7ee06871be5d3b89e31
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
flutter_image_compress: 082f8daaf6c1b0c9fe798251c750ef0ecd98d7ae
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
GoogleDataTransport: e85fb700c9b027079ce182c3d08e12e0f9618bb4 GoogleDataTransport: e85fb700c9b027079ce182c3d08e12e0f9618bb4
GoogleUtilities: 7f2f5a07f888cdb145101d6042bc4422f57e70b3 GoogleUtilities: 7f2f5a07f888cdb145101d6042bc4422f57e70b3
libwebp: 946cb3063cea9236285f7e9a8505d806d30e07f3
Mantle: bf2e1b464f82465b9074e9de970739a2615e093a
MTBBarcodeScanner: f453b33c4b7dfe545d8c6484ed744d55671788cb MTBBarcodeScanner: f453b33c4b7dfe545d8c6484ed744d55671788cb
nanopb: 59317e09cf1f1a0af72f12af412d54edf52603fc nanopb: 59317e09cf1f1a0af72f12af412d54edf52603fc
package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62 package_info: 873975fc26034f0b863a300ad47e7f1ac6c7ec62
Protobuf: 3dac39b34a08151c6d949560efe3f86134a3f748 Protobuf: 3dac39b34a08151c6d949560efe3f86134a3f748
SDWebImage: a990c053fff71e388a10f3357edb0be17929c9c5 SDWebImage: a990c053fff71e388a10f3357edb0be17929c9c5
SDWebImageFLPlugin: 6c2295fb1242d44467c6c87dc5db6b0a13228fd8 SDWebImageFLPlugin: 6c2295fb1242d44467c6c87dc5db6b0a13228fd8
SDWebImageWebPCoder: d0dac55073088d24b2ac1b191a71a8f8d0adac21
shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d shared_preferences: af6bfa751691cdc24be3045c43ec037377ada40d
sqflite: 4001a31ff81d210346b500c55b17f4d6c7589dd0 sqflite: 4001a31ff81d210346b500c55b17f4d6c7589dd0
SwiftProtobuf: 4ef85479c18ca85b5482b343df9c319c62bda699 SwiftProtobuf: 4ef85479c18ca85b5482b343df9c319c62bda699

+ 20
- 21
lib/presentation/custom_widgets/widget_media_picker.dart View File

import 'package:farm_tpf/custom_model/Media.dart'; import 'package:farm_tpf/custom_model/Media.dart';
import 'package:farm_tpf/presentation/custom_widgets/camera_helper.dart'; import 'package:farm_tpf/presentation/custom_widgets/camera_helper.dart';
import 'package:farm_tpf/presentation/custom_widgets/widget_show_video.dart'; import 'package:farm_tpf/presentation/custom_widgets/widget_show_video.dart';
import 'package:farm_tpf/presentation/screens/actions/util_action.dart';
import 'package:farm_tpf/utils/const_color.dart'; import 'package:farm_tpf/utils/const_color.dart';
import 'package:farm_tpf/utils/const_common.dart'; import 'package:farm_tpf/utils/const_common.dart';
import 'package:farm_tpf/utils/const_string.dart'; import 'package:farm_tpf/utils/const_string.dart';
await FilePicker.platform.pickFiles(type: FileType.image); await FilePicker.platform.pickFiles(type: FileType.image);


if (result != null) { if (result != null) {
String filePath = result.files.single.path;
var lengthFileInBytes = result.files.single.size * 1000;
if (lengthFileInBytes > ConstCommon.kFileSize) {
Get.snackbar(label_file_to_large,
"Kích thước: $lengthFileInBytes bytes",
snackPosition: SnackPosition.BOTTOM);
} else {
Media newMedia = Media()
..isVideo = false
..isServerFile = false
..pathFile = filePath;
currentItems.add(newMedia);
files.add(filePath);
BlocProvider.of<MediaHelperBloc>(context)
..add(ChangeListMedia(items: currentItems));
widget.onChangeFiles(files);
}
print("file size: $lengthFileInBytes");
UtilAction.compressImage(File(result.files.single.path))
.then((compressFile) async {
var lengthFileInBytes = await compressFile.length();
if (lengthFileInBytes > ConstCommon.kFileSize) {
Get.snackbar(label_file_to_large, label_file_size_suggest,
snackPosition: SnackPosition.BOTTOM);
} else {
Media newMedia = Media()
..isVideo = false
..isServerFile = false
..pathFile = compressFile.path;
currentItems.add(newMedia);
files.add(compressFile.path);
BlocProvider.of<MediaHelperBloc>(context)
..add(ChangeListMedia(items: currentItems));
widget.onChangeFiles(files);
}
});
} }
}), }),
CupertinoDialogAction( CupertinoDialogAction(
String filePath = result.files.single.path; String filePath = result.files.single.path;
var lengthFileInBytes = result.files.single.size * 1000; var lengthFileInBytes = result.files.single.size * 1000;
if (lengthFileInBytes > ConstCommon.kFileSize) { if (lengthFileInBytes > ConstCommon.kFileSize) {
Get.snackbar(label_file_to_large,
"Kích thước: $lengthFileInBytes bytes",
Get.snackbar(label_file_to_large, label_file_size_suggest,
snackPosition: SnackPosition.BOTTOM); snackPosition: SnackPosition.BOTTOM);
} else { } else {
Media newMedia = Media() Media newMedia = Media()
..add(ChangeListMedia(items: currentItems)); ..add(ChangeListMedia(items: currentItems));
widget.onChangeFiles(files); widget.onChangeFiles(files);
} }
print("file size: $lengthFileInBytes");
} }
}), }),
CupertinoDialogAction( CupertinoDialogAction(

+ 20
- 0
lib/presentation/screens/actions/util_action.dart View File

import 'dart:io';

import 'package:farm_tpf/custom_model/Media.dart'; import 'package:farm_tpf/custom_model/Media.dart';
import 'package:farm_tpf/utils/const_common.dart'; import 'package:farm_tpf/utils/const_common.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:mime/mime.dart'; import 'package:mime/mime.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:path/path.dart' as path;


class UtilAction { class UtilAction {
static Future<List<Media>> cacheFiles(String existedMedias) async { static Future<List<Media>> cacheFiles(String existedMedias) async {
} }
return result; return result;
} }

static Future<File> compressImage(File file) async {
final dir = await path_provider.getTemporaryDirectory();
final fileName = path.basenameWithoutExtension(file.path);
final targetPath = dir.absolute.path + "/$fileName.jpg";
final imgFile = await compressAndGetFile(file, targetPath);
return imgFile;
}

static Future<File> compressAndGetFile(File file, String targetPath) async {
final result = await FlutterImageCompress.compressAndGetFile(
file.absolute.path, targetPath,
minWidth: 1024, minHeight: 1024);
return result;
}
} }

+ 2
- 1
lib/utils/const_string.dart View File

const String label_update_success = "Cập nhật thành công"; const String label_update_success = "Cập nhật thành công";
const String label_add_success = "Thêm thành công"; const String label_add_success = "Thêm thành công";


const String label_file_to_large = "Kích thước hình/video quá lớn";
const String label_file_to_large = "Kích thước tập tin quá lớn";
const String label_file_size_suggest = "Vui lòng chọn tập tin nhỏ hơn 1024 Kb";


//Exception //Exception
const String exception_common = "Đã có lỗi xảy ra"; const String exception_common = "Đã có lỗi xảy ra";

+ 7
- 0
pubspec.lock View File

url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.4.0" version: "1.4.0"
flutter_image_compress:
dependency: "direct main"
description:
name: flutter_image_compress
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.0"
flutter_plugin_android_lifecycle: flutter_plugin_android_lifecycle:
dependency: transitive dependency: transitive
description: description:

+ 1
- 0
pubspec.yaml View File

flutter_cache_manager: ^1.4.2 flutter_cache_manager: ^1.4.2


mime: ^0.9.7 mime: ^0.9.7
flutter_image_compress: ^0.7.0


dev_dependencies: dev_dependencies:
flutter_test: flutter_test:

Loading…
Cancel
Save