Created
December 11, 2018 06:47
-
-
Save shyjuzz/d3da12dc5773d5ce5a957da61707de51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| import 'dart:io' as Io; | |
| import 'package:image/image.dart'; | |
| import 'package:flutter_cache_manager/flutter_cache_manager.dart'; | |
| import 'package:path_provider/path_provider.dart'; | |
| class SaveFile { | |
| Future<String> get _localPath async { | |
| final directory = await getApplicationDocumentsDirectory(); | |
| return directory.path; | |
| } | |
| Future<Io.File> getImageFromNetwork(String url) async { | |
| var cacheManager = await CacheManager.getInstance(); | |
| Io.File file = await cacheManager.getFile(url); | |
| return file; | |
| } | |
| Future<Io.File> saveImage(String url) async { | |
| final file = await getImageFromNetwork(url); | |
| //retrieve local path for device | |
| var path = await _localPath; | |
| Image image = decodeImage(file.readAsBytesSync()); | |
| Image thumbnail = copyResize(image, 120); | |
| // Save the thumbnail as a PNG. | |
| return new Io.File('$path/${DateTime.now().toUtc().toIso8601String()}.png') | |
| ..writeAsBytesSync(encodePng(thumbnail)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment