Search code examples
flutterflutter-dependenciesflutter-image-picker

flutter image_picker app crash after select image


I'm using image picker and get this error after I select an image from gallery.

E/AndroidRuntime( 1465): FATAL EXCEPTION: main

E/AndroidRuntime( 1465): Process: com.hoeis.report_project, PID: 1465

E/AndroidRuntime( 1465): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2346, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:241387 flg=0x1 }} to activity {com.hoeis.report_project/com.hoeis.report_project.MainActivity}: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

E/AndroidRuntime( 1465):    at android.app.ActivityThread.deliverResults(ActivityThread.java:4761)

E/AndroidRuntime( 1465):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4803)

E/AndroidRuntime( 1465):    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)

E/AndroidRuntime( 1465):    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)

E/AndroidRuntime( 1465):    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)

E/AndroidRuntime( 1465):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2029)

E/AndroidRuntime( 1465):    at android.os.Handler.dispatchMessage(Handler.java:106)

E/AndroidRuntime( 1465):    at android.os.Looper.loop(Looper.java:233)

E/AndroidRuntime( 1465):    at android.app.ActivityThread.main(ActivityThread.java:7212)

E/AndroidRuntime( 1465):    at java.lang.reflect.Method.invoke(Native Method)

E/AndroidRuntime( 1465):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:499)

E/AndroidRuntime( 1465):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

E/AndroidRuntime( 1465): Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1

E/AndroidRuntime( 1465):    at java.lang.String.substring(String.java:2036)

E/AndroidRuntime( 1465):    at io.flutter.plugins.imagepicker.FileUtils.getBaseName(FileUtils.java:134)

E/AndroidRuntime( 1465):    at io.flutter.plugins.imagepicker.FileUtils.getPathFromUri(FileUtils.java:70)

E/AndroidRuntime( 1465):    at io.flutter.plugins.imagepicker.ImagePickerDelegate.handleChooseMultiImageResult(ImagePickerDelegate.java:584)

E/AndroidRuntime( 1465):    at io.flutter.plugins.imagepicker.ImagePickerDelegate.onActivityResult(ImagePickerDelegate.java:547)

E/AndroidRuntime( 1465):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:805)

E/AndroidRuntime( 1465):    at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:428)

E/AndroidRuntime( 1465):    at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:807)

E/AndroidRuntime( 1465):    at io.flutter.embedding.android.FlutterActivity.onActivityResult(FlutterActivity.java:724)

E/AndroidRuntime( 1465):    at android.app.Activity.dispatchActivityResult(Activity.java:7741)

E/AndroidRuntime( 1465):    at android.app.ActivityThread.deliverResults(ActivityThread.java:4754)

E/AndroidRuntime( 1465):    ... 11 more

I/Process ( 1465): Sending signal. PID: 1465 SIG: 9

Lost connection to device.

this is the code that called the image picker

customButton(context, isLoading, "Get Image", () async {
              try {
                final pickedFile = await picker.pickMultiImage(
                    imageQuality: 100, maxHeight: 1000, maxWidth: 1000);
                print(pickedFile.path.toString());
              } catch (e) {
                print("error: $e");
              }
            })

this is my packages that i used in pubspec.yaml

  flutter_launcher_icons: ^0.12.0
  parse_server_sdk_flutter: ^4.0.0
  dio: ^5.0.3
  shared_preferences: ^2.0.18
  path: ^1.8.2
  path_provider: ^2.0.13
  camera: ^0.10.3+2
  image_picker: ^0.8.7
  ntp: ^2.0.0
  permission_handler: ^10.2.0
  flutter_riverpod: ^2.3.2
  intl: ^0.18.0
  geocoding: ^2.1.0
  geolocator: ^9.0.2

I tried to disable all the packages except the image picker thinking there was a package conflict but it still doesn't work. for permission, I also handle manually using permission_handler and already checked all of them granted for camera and storage so it should not be a permission problem. On my other project using image picker it's worked fine with no problem at all, this makes me confused just what is wrong with the current one


Solution

  • I finally managed to make it work, package image_picker seems to have a problem with parse_server_sdk. Because after I changed my backend I didn't use parse_server_sdk anymore and I tried again using image_picker with version ^0.8.9 and it worked. either the developer has fixed it on the new version or because it has a compatibility problem with parse_server_sdk