Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compressing a Picture in flutter returns null

I am trying to compress an image in Flutter and scale it to a certain format (16/9)
For the compression I use the library FlutterImageCompress.
However, this gives me a return value of zero.
The documentation says:
"Sometimes, compressing will return null. You should check if you can read/write the file, and the parent folder of the target file must exist.

For example, use the path_provider plugin to access some application folders, and use a permission plugin to request permission to access SD cards on Android/iOS." https://pub.dev/packages/flutter_image_compress#troubleshooting

So I tried to create the folder before compressing. My code then looked like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

final appDocDir = await getApplicationDocumentsDirectory();
        pickedFile = File(filepath); //retrieved with filepicker

        Directory tempDir = Directory('${appDocDir.path}/temp/');
        if (!tempDir.existsSync()) {
          tempDir.createSync(recursive: true);
        }

        pickedFile = await FlutterImageCompress.compressAndGetFile(
          pickedFile.absolute.path,
          "${appDocDir.absolute}/temp.jpg",
          quality: 5,
        );
        File file = File("${appDocDir.absolute}/temp.jpg");
        print("File Size: ${file.lengthSync()}");

My goal is that the File file contains the compressed file at the end, so that I can upload it.

I have already tried to create the folder in which the result should be written manually, but this has not changed anything. I also don’t care where the image ends up after compression, as it is deleted again after the file is uploaded.

>Solution :

As already shown in this solution, you can also implement it. https://stackoverflow.com/a/64690920

You can simply save the new file on the same path as the original file you want to compress. This way you can ensure that you have write permissions in this location.

The whole thing would be as follows for your code:

final appDocDir = await getApplicationDocumentsDirectory();
        pickedFile = File(ref.watch(imageProvider).image!.path!);
        
        final filePath = pickedFile.absolute.path;
        final lastIndex = filePath.lastIndexOf(new RegExp(r'.jp'));
        final splitted = filePath.substring(0, (lastIndex));
        final outPath = "${splitted}_out${filePath.substring(lastIndex)}";

        await FlutterImageCompress.compressAndGetFile(
          pickedFile.absolute.path,
          outPath,
          quality: 5,
        );
        File file = File(outPath);
        print("File Size: ${file.lengthSync()}");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading