1

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

  • usually it only problem with storage/camera permission, but as you say you already checked it. try to create a new project and create a simple camera using `image_picker`, just incase you wrongly implement it – Stanly Mar 26 '23 at 04:08
  • @Stanly I still got the same error sir, even after creating a new project just taking a picture from the gallery `java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2346, result=-1, data=Intent { flg=0x1 clip={image/* U:content://com.android.providers.media.documents/document/image%3A789022 ...} }} to activity {com.example.flutter_image_picker_test/com.example.flutter_image_picker_test.MainActivity}: java.lang.StringIndexOutOfBoundsException: String index out of range: -1` – ChristianPWH Work Mar 26 '23 at 16:16
  • Not sure if you're still working on this. Have you figured it out yet? I'm running into the same issue. For me, I noticed this happens when I pick an image that is missing the file extension. So for example "picture.jpeg" is actually saved as "picture". The picker has no problem displaying the file, but if you pick it, it crashes. If you look at the call stack, last call is to FileUtils.getBaseName, and I believe that is trying to get the extension using substring, but because it's not there it just throws and crashes because it's not handled. Seems like a lib bug too me.... :( – ShrimpCrackers Apr 12 '23 at 22:24
  • @ShrimpCrackers well, I am still open to suggestions but for my current project, I'm using another package https://pub.dev/packages/images_picker. for me, it's not just an image without an extension the normal one also got the error. – ChristianPWH Work Apr 30 '23 at 18:00

1 Answers1

0

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