flutterhybrid-mobile-app

I can't crop a Picture in FLUTTER GETTING TYPE ERROR OR THE APP CRASHES


import 'dart:io';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:project1_test/theme/Textstyle.dart';
import 'package:project1_test/theme/colors.dart';
import 'package:image_cropper/image_cropper.dart';

class ImageSelectionfunction extends StatefulWidget {
  const ImageSelectionfunction({super.key});

  @override
  State<ImageSelectionfunction> createState() => _ImageSelectionfunctionState();
}

class _ImageSelectionfunctionState extends State<ImageSelectionfunction> {
  File? SelectedImage;

  @override
  Widget build(BuildContext context) {
     ///////////////////////////////////////////////////
    ///////////////////////////
    //Function To Crop Image
    ///////////////////////////
    Future<void> cropImage() async {
      final croppedImage =
          ImageCropper().cropImage(sourcePath: SelectedImage!.path);
      if (SelectedImage != null) {
        final croppedImage = await ImageCropper()
            .cropImage(sourcePath: SelectedImage!.path, aspectRatioPresets: [
          CropAspectRatioPreset.square,
          CropAspectRatioPreset.ratio3x2,
          CropAspectRatioPreset.original,
          CropAspectRatioPreset.ratio4x3,
          CropAspectRatioPreset.ratio16x9
        ]);
        uiSettings:
        AndroidUiSettings(
          toolbarTitle: 'Crop the Picture',
          toolbarColor: purpleColor,
          toolbarWidgetColor: Colors.white,
          initAspectRatio: CropAspectRatioPreset.original,
          lockAspectRatio: false,
        );
        IOSUiSettings(
          title: 'Crop the Picture',
        );
        if (croppedImage != null) {
          SelectedImage = File(croppedImage.path);
        }
      }
    }

    return Column(children: <Widget>[
      Center(
          child: Container(
        width: 220,
        height: 190,
        decoration: const BoxDecoration(color: Colors.white, boxShadow: [
          BoxShadow(
            color: Colors.grey,
            offset: Offset(0, 4),
            blurRadius: 5,
            spreadRadius: 5,
          )
        ]),
        child: SelectedImage != null
            ? Image.file(SelectedImage!)
            : Center(child: const Text('Your Image here')),
      )),
      SizedBox(
        height: 35,
      ),
      //////////////////////////////
      //Image Selection Buttons Ends Here
      ///////////////////////////////
      SizedBox(
        height: 12,
      ),
      /////////////////////
      /////Cropping and editing
      //////////////////
      Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          IconButton(
              onPressed: () {
                cropImage();
              },
              icon: Icon(FontAwesomeIcons.penToSquare)),
          Text(
            'Crop',
            style: Secondarytxt,
          )
        ],
      ),
      ///////////////////////
      //Prediction Button
      ///////////////////////
      SizedBox(
        height: 12,
      ),
      Container(
        width: 180,
        height: 60,
        color: purpleColor,
        child: Row(children: [
          Padding(
            padding: const EdgeInsets.only(left: 10),
            child: Icon(
              FontAwesomeIcons.magnifyingGlass,
              color: Colors.white,
              size: 18,
            ),
          ),
          SizedBox(
            width: 12,
          ),
          Text(
            'Start Prediction',
            style: txtStylemain,
          ),
        ]),
      )
      //////////////////////////////
      //Prediction Button Ends Here
      ////////////////////////////
    ]);
  }
}

I am trying to add Image cropper but the app crashes when i fix the invalid assignment error the code is suppose to crop an uploaded image and save the new image to SelectedImage Variable but i am encountering with appcrash with little to no information in the debug console... and i am also getting type error on SelectedImage=croppedImage. The Select Image from Gallery and capture Image using Camera functions are working as expected but this part doesn't seem to work

this is the error messages on my debug console

E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): Failed to handle method call E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.project1_test/com.yalantis.ucrop.UCropActivity}; have you declared this activity in your AndroidManifest.xml? E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2005) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Activity.startActivityForResult(Activity.java:4586) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Activity.startActivityForResult(Activity.java:4544) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at vn.hunghd.flutter.plugins.imagecropper.ImageCropperDelegate.startCrop(ImageCropperDelegate.java:99) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at vn.hunghd.flutter.plugins.imagecropper.ImageCropperPlugin.onMethodCall(ImageCropperPlugin.java:59) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:322) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.os.Handler.handleCallback(Handler.java:873) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.os.Handler.dispatchMessage(Handler.java:99) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.os.Looper.loop(Looper.java:193) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.ActivityThread.main(ActivityThread.java:6669) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at java.lang.reflect.Method.invoke(Native Method) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): Failed to handle method call E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.project1_test/com.yalantis.ucrop.UCropActivity}; have you declared this activity in your AndroidManifest.xml? E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): ivityResult(Instrumentation.java:2005) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Activity.startActivityForResult(Activity.java:4586) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at android.app.Activity.startActivityForResult(Activity.java:4544) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at vn.hunghd.flutter.plugins.imagecropper.ImageCropperDelegate.startCrop(ImageCropperDelegate.java:99) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at vn.hunghd.flutter.plugins.imagecropper.ImageCropperPlugin.onMethodCall(ImageCropperPlugin.java:59) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:267) E/MethodChannel#plugins.hunghd.vn/image_cropper( 2860): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295) E/flutter ( 2860): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Unable to find explicit activity class {com.example.project1_test/com.yalantis.ucrop.UCropActivity}; have you declared this activity in your AndroidManifest.xml?, null, android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.project1_test/com.yalantis.ucrop.UCropActivity}; have you declared this activity in your AndroidManifest.xml? E/flutter ( 2860): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2005) E/flutter ( 2860): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673) E/flutter ( 2860): at android.app.Activity.startActivityForResult(Activity.java:4586) E/flutter ( 2860): at android.app.Activity.startActivityForResult(Activity.java:4544) E/flutter ( 2860): at vn.hunghd.flutter.plugins.imagecropper.ImageCropperDelegate.startCrop(ImageCropperDelegate.java:99) E/flutter ( 2860): at vn.hunghd.flutter.plugins.imagecropper.ImageCropperPlugin.onMethodCall(ImageCropperPlugin.java:59)


Solution

  • It seems like the error is related to the Image Cropper plugin, specifically the UCropActivity not being declared in your AndroidManifest.xml. Please Add UCropActivity into your AndroidManifest.xml.

    <activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>