Repository: flutter
Issue: [camera] java.lang.ArrayIndexOutOfBoundsException when seeking multiple permissions
Labels: c: crash,platform-android,p: camera,package,has reproducible steps,P2,found in release: 2.10,found in release: 2.13,team-android,triaged-android

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. Allow camera usage

**Expected results:** Camera preview shows up

**Actual results:** Black preview. `ArrayIndexOutOfBoundsException` in debug log.

```
E/MethodChannel#plugins.flutter.io/camera( 1934): Failed to handle method call
E/MethodChannel#plugins.flutter.io/camera( 1934): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.plugins.camera.CameraPermissions$CameraRequestPermissionsListener.onRequestPermissionsResult(CameraPermissions.java:92)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onRequestPermissionsResult(FlutterEngineConnectionRegistry.java:812)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onRequestPermissionsResult(FlutterEngineConnectionRegistry.java:420)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onRequestPermissionsResult(FlutterActivityAndFragmentDelegate.java:722)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.android.FlutterActivity.onRequestPermissionsResult(FlutterActivity.java:709)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at android.app.Activity.requestPermissions(Activity.java:5278)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at androidx.core.app.ActivityCompat.requestPermissions(ActivityCompat.java:516)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.plugins.camera.CameraPermissions.requestPermissions(CameraPermissions.java:45)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.plugins.camera.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:73)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:296)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$DartMessenger(DartMessenger.java:320)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at io.flutter.embedding.engine.dart.-$$Lambda$DartMessenger$TsixYUB5E6FpKhMtCSQVHKE89gQ.run(Unknown Source:12)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at android.os.Looper.loop(Looper.java:246)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at android.app.ActivityThread.main(ActivityThread.java:8653)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
E/MethodChannel#plugins.flutter.io/camera( 1934): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
```

This happens because the app triggers a permission request before the camera plugin so its request failed:

```
W/Activity( 1934): Can request only one set of permissions at a time
```

When the `onRequestPermissionsResult` is called, it didn't realize the incoming `permissions` and `grantResults` arrays are both empty. Ideally, this method should map the permission and result together instead of checking result array blindly. 

<details>
<summary>Code sample</summary>

```dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:permission_handler/permission_handler.dart';

late List<CameraDescription> cameras;

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  cameras = await availableCameras();
  runApp(const CameraApp());
}

class CameraApp extends StatefulWidget {
  const CameraApp({Key? key}) : super(key: key);

  @override
  _CameraAppState createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  CameraController? _controller;

  @override
  void initState() {
    super.initState();

    _checkCameraPermission().then((_) {
      _initCameraController();
    });
  }

  Future<void> _checkCameraPermission() async {
    var status = await Permission.camera.status;
    if (status.isDenied) {
      Permission.camera.request();
    }
  }

  void _initCameraController() async {
    final controller = _controller = CameraController(
      cameras[0],
      ResolutionPreset.max,
      enableAudio: false,
    );

    await controller.initialize();

    if (!mounted) return;
    setState(() {});
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final controller = _controller;
    if (controller == null || !controller.value.isInitialized) {
      return Container();
    }

    return MaterialApp(
      home: CameraPreview(controller),
    );
  }
}
```

</details>

<details>
  <summary>Logs</summary>

Verbose log is too long, see it in full at https://gist.github.com/daohoangson/d6b37b3ecebf3e7c948207730cfbe4e8

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
Analyzing bug...                              
No issues found! (ran in 2.4s)
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 2.10.3, on macOS 11.3 20E232 darwin-x64, locale en)
    • Flutter version 2.10.3 at /Users/sondh/fvm/versions/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7e9793dee1 (7 weeks ago), 2022-03-02 11:23:12 -0600
    • Engine revision bd539267b4
    • Dart version 2.16.1
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc2)
    • Android SDK at /Users/sondh/Library/Android/sdk
    • Platform android-31, build-tools 33.0.0-rc2
    • ANDROID_HOME = /Users/sondh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.66.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.38.1

[✓] Connected device (3 available)
    • SM A225F (mobile) • R58RC4J907E • android-arm64  • Android 11 (API 30)
    • macOS (desktop)   • macos       • darwin-x64     • macOS 11.3 20E232 darwin-x64
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 100.0.4896.127

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->