@@ -249,11 +249,28 @@ class _MyAppState extends State<MyApp> {
249
249
});
250
250
}
251
251
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.
252
257
void _compressHeicExample () async {
253
258
print ('start compress' );
254
259
final logger = TimeLogger ();
255
260
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
+
257
274
final target = '$tmpDir /${DateTime .now ().millisecondsSinceEpoch }.heic' ;
258
275
final srcPath = await getExampleFilePath ();
259
276
final result = await FlutterImageCompress .compressAndGetFile (
@@ -272,6 +289,30 @@ class _MyAppState extends State<MyApp> {
272
289
'Compress heic result path: ${result .path }, '
273
290
'size: ${await result .length ()}' ,
274
291
);
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
+ }
275
316
}
276
317
277
318
void _compressAndroidWebpExample () async {
0 commit comments