Skip to content

ANR: IndexedOutOfBoundsException when picking image file without an extension #124821

@aloh86

Description

@aloh86

Is there an existing issue for this?

Steps to reproduce

I discovered this issue as I was using image_picker to pick a photo that Android asked me to rename before downloading because the image name was so long (given a GUID of some sort). I renamed the photo not knowing I had to include the extension. When I picked the image using image_picker the app crashed.

Important prerequisite to reproduce
This issue only happens on certain versions of Android (e.g. Android 7.0/API 24). Certain versions of the Android picker allow images to be renamed without an extension. Other, newer versions of the allow renaming without an extension, but the file is no longer considered an image and not visible in the image picker without a valid image extension (e.g. jpg or png).

  1. On mobile device or emulator (preferably with Android 7.0/API 24), open web browser and download a jpg or png image from the internet.
  2. Rename the file so that the extension including the dot is removed (e.g. save "image.jpg" as "image").
  3. From development device download simplified code repo at:
  4. Run app and pick the download image in step 1.
  5. Observe app crash.

Issue occurs on both flutter channel stable and flutter channel master.

Expected results

File should be picked with no ANR/exception.

Actual results

An IndexOutOfBoundsException is being thrown and never caught causing app to crash.

Code sample

Code sample Requires installation of image_picker plugin.
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'image picker ext. bug',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Extension bug demo"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Download a file jpg or png file and rename it with no extension (e.g. rename image.jpg to image).'
  'Picking that image with the image_picker will crash the app.',
            ),
            ElevatedButton(
                onPressed: () async {
                  final ImagePicker picker = ImagePicker();
                  final XFile? image = await picker.pickImage(source: ImageSource.gallery);
                },
                child: Text("Pick image")
            )
          ],
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
device-2023-04-04-211959.webm

Logs

Logs
E/AndroidRuntime( 3761): FATAL EXCEPTION: main
E/AndroidRuntime( 3761): Process: com.example.image_picker_ext_bug, PID: 3761
E/AndroidRuntime( 3761): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2342, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/5 flg=0x1 }} to activity {com.example.image_picker_ext_bug/com.example.image_picker_ext_bug.MainActivity}: java.lang.StringIndexOutOfBoundsException: length=8; regionStart=0; regionLength=-1
E/AndroidRuntime( 3761): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
E/AndroidRuntime( 3761): 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
E/AndroidRuntime( 3761): 	at android.app.ActivityThread.-wrap20(ActivityThread.java)
E/AndroidRuntime( 3761): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
E/AndroidRuntime( 3761): 	at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 3761): 	at android.os.Looper.loop(Looper.java:154)
E/AndroidRuntime( 3761): 	at android.app.ActivityThread.main(ActivityThread.java:6077)
E/AndroidRuntime( 3761): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 3761): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/AndroidRuntime( 3761): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
E/AndroidRuntime( 3761): Caused by: java.lang.StringIndexOutOfBoundsException: length=8; regionStart=0; regionLength=-1
E/AndroidRuntime( 3761): 	at java.lang.String.substring(String.java:1931)
E/AndroidRuntime( 3761): 	at io.flutter.plugins.imagepicker.FileUtils.getBaseName(FileUtils.java:134)
E/AndroidRuntime( 3761): 	at io.flutter.plugins.imagepicker.FileUtils.getPathFromUri(FileUtils.java:70)
E/AndroidRuntime( 3761): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate.handleChooseImageResult(ImagePickerDelegate.java:567)
E/AndroidRuntime( 3761): 	at io.flutter.plugins.imagepicker.ImagePickerDelegate.onActivityResult(ImagePickerDelegate.java:544)
E/AndroidRuntime( 3761): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:807)
E/AndroidRuntime( 3761): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:430)
E/AndroidRuntime( 3761): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:811)
E/AndroidRuntime( 3761): 	at io.flutter.embedding.android.FlutterActivity.onActivityResult(FlutterActivity.java:771)
E/AndroidRuntime( 3761): 	at android.app.Activity.dispatchActivityResult(Activity.java:6915)
E/AndroidRuntime( 3761): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:4049)

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.7.9, on Microsoft Windows [Version 10.0.19044.2728], locale en-US)
This is taking an unexpectedly long time...[✓] Windows Version (Installed version of Windows is version 10 or higher)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Chrome - develop for the web
[!] Visual Studio - develop for Windows (Visual Studio Community 2022 17.1.3)
    ✗ Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop
      development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK
[✓] Android Studio (version 2022.1)
[✓] VS Code (version 1.76.0)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listc: crashStack traces logged to the consolec: fatal crashCrashes that terminate the processp: image_pickerThe Image Picker plugin.packageflutter/packages repository. See also p: labels.platform-androidAndroid applications specifically

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions