Skip to content

Commit 70b8242

Browse files
committed
chore: update example
Signed-off-by: Caijinglong <[email protected]>
1 parent 5af0ee5 commit 70b8242

File tree

1 file changed

+42
-1
lines changed
  • packages/flutter_image_compress/example/lib/main

1 file changed

+42
-1
lines changed

packages/flutter_image_compress/example/lib/main/main_io.dart

+42-1
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,28 @@ class _MyAppState extends State<MyApp> {
249249
});
250250
}
251251

252+
/// The example for compressing heic format.
253+
///
254+
/// Convert jpeg to heic format, and then convert heic to jpg format.
255+
///
256+
/// Show the file path and size in the console.
252257
void _compressHeicExample() async {
253258
print('start compress');
254259
final logger = TimeLogger();
255260
logger.startRecorder();
256-
final tmpDir = (await getTemporaryDirectory()).path;
261+
// final tmpDir = (await getTemporaryDirectory()).path;
262+
final tmpDir = (await path_provider.getExternalStorageDirectories())
263+
?.first
264+
.absolute
265+
.path;
266+
267+
if (tmpDir == null) {
268+
print('tmpDir is null');
269+
print(
270+
'You need check your permission for the external storage on Android.');
271+
return;
272+
}
273+
257274
final target = '$tmpDir/${DateTime.now().millisecondsSinceEpoch}.heic';
258275
final srcPath = await getExampleFilePath();
259276
final result = await FlutterImageCompress.compressAndGetFile(
@@ -272,6 +289,30 @@ class _MyAppState extends State<MyApp> {
272289
'Compress heic result path: ${result.path}, '
273290
'size: ${await result.length()}',
274291
);
292+
293+
// Convert heic to jpg
294+
final jpgPath =
295+
'$tmpDir/heic-to-jpg-${DateTime.now().millisecondsSinceEpoch}.jpg';
296+
try {
297+
final jpgResult = await FlutterImageCompress.compressAndGetFile(
298+
result.path,
299+
jpgPath,
300+
format: CompressFormat.jpeg,
301+
quality: 90,
302+
);
303+
if (jpgResult == null) {
304+
print('Convert heic to jpg failed.');
305+
} else {
306+
print(
307+
'Convert heic to jpg success. '
308+
'Jpg path: ${jpgResult.path}, '
309+
'size: ${await jpgResult.length()}',
310+
);
311+
}
312+
} catch (e) {
313+
print('Error: $e');
314+
print('Convert heic to jpg failed.');
315+
}
275316
}
276317

277318
void _compressAndroidWebpExample() async {

0 commit comments

Comments
 (0)