LiveRock
LiveRock

Reputation: 1033

Flutter/Dart resizing an Image in actual device took > 10 mins

I discovered a behaviour with Flutter/Dart. I am trying to resize an image from the ImagePicker. The simulator works well but on the actual device, an iPhone 6 plus, the whole process took more than 10 mins and ended with a crash.

On the actual device, I clicked on the button that brings out the Image Picker, I select a photo and the device just hung. After 10 mins, the Image Pciker dismisses and continues with the image resizing and after 5 mins or so, it crashes.

Here is the code:

ImagePicker.pickImage(source: source)
      .then((_imageFile2) => _uploadFile(_imageFile2)
      .then((downbloadURL) {
          if (downbloadURL != null ) {

              createCloudStoreRecord(fireBaseUser, downbloadURL, true);

              setState(() {
                  profileImage = new DecorationImage(
                  image: getProfileImage(downbloadURL), 
                  fit: BoxFit.cover,
                  );

              });
            Navigator.pop(context);
            showInSnackBar("Image Updated");
          } else {
            Navigator.pop(context);
            showInSnackBar("Image Update Error!");
          }
      }));


Future<String> _uploadFile(_imageFile2) async {
    print("in upload image");

    if (_imageFile2==null) {
        print("imagePicker image is null");
        Navigator.pop(context);
        return null;
    } else {
        onLoading(context, "Updating ...");

        try {
          // resize image
          Im.Image image = Im.decodeImage(_imageFile2.readAsBytesSync());
          Im.Image smallerImage = Im.copyResize(image, 500); // choose the size here, it will maintain aspect ratio
          final tempDir = await getTemporaryDirectory();
          final path = tempDir.path;
          var filename = user.uid.toString() + ".png";
          var newPath = '$path/' + filename;
          print("start compressed");
          var compressedImage = new File(newPath)..writeAsBytesSync(Im.encodePng(smallerImage));
          print("end compressed");
          //final Directory systemTempDir = Directory.systemTemp;
          final StorageReference ref = FirebaseStorage.instance.ref().child('users/' + filename);
          final StorageUploadTask uploadTask = ref.putFile(
            compressedImage,
            new StorageMetadata(
              contentLanguage: 'en',
              customMetadata: <String, String>{'renalbase': 'user_photo'},
            ),
          );
          print("Start upload");
          UploadTaskSnapshot uploadSnapshot = await uploadTask.future;

          print("image uploaded");

          Map<String, dynamic> pictureData = new Map<String, dynamic>();
          pictureData["url"] = uploadSnapshot.downloadUrl.toString();
          print("Bfore url = ${pictureData["url"]}");
          final RegExp regExp = RegExp('.*(?=\\?)');
          pictureData["url"] = Uri.decodeFull( regExp.stringMatch(pictureData["url"]) );
          print("url = ${pictureData["url"]}");
          return pictureData["url"];
        } catch(e) {
            print("Upload error: $e");
            showInSnackBar("Upload error: $e");
            return null;
          }
      }

  }

Upvotes: 3

Views: 5295

Answers (3)

Pierre Canthelou
Pierre Canthelou

Reputation: 1

Neither flutter_native_image or ImagePicker works well in all cases...

flutter_native_image resize the picture by x-percent when you just call the quality parameter... what a mess, this is not the intended purpose !!!

and image_picker on android does not resize pictures when you provide two max dimensions and one dimension is lesser than one provided...

Upvotes: 0

Jens R.
Jens R.

Reputation: 178

I had similar issues with image resizing taking too long. I switched to using the maxHeight and maxWidth parameters in ImagePicker.pickImage and have had far better results.

_imageFile = await ImagePicker.pickImage(
          source: ImageSource.gallery, 
          maxHeight: 450.0, 
          maxWidth: 450.0);

Upvotes: 7

LiveRock
LiveRock

Reputation: 1033

Gave up on the image plugin and used the flutter_native_image instead. (https://github.com/btastic/flutter_native_image)

Works like a charm.

Upvotes: 5

Related Questions