diff --git a/packages/camera/CHANGELOG.md b/packages/camera/CHANGELOG.md index 3f6872d79ad4..1c36dd824597 100644 --- a/packages/camera/CHANGELOG.md +++ b/packages/camera/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.5.1 + +* Can now be compiled with earlier Android sdks below 21 when +`` has been added to the project +`AndroidManifest.xml`. For sdks below 21, the plugin won't be registered and calls to it will throw +a `MissingPluginException.` + ## 0.5.0 * **Breaking Change** This plugin no longer handles closing and opening the camera on Android diff --git a/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java b/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java index 3258993e49f4..54243ed28a66 100644 --- a/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java +++ b/packages/camera/android/src/main/java/io/flutter/plugins/camera/CameraPlugin.java @@ -82,11 +82,13 @@ public void onOrientationChanged(int i) { } public static void registerWith(Registrar registrar) { - if (registrar.activity() == null) { + if (registrar.activity() == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // When a background flutter view tries to register the plugin, the registrar has no activity. - // We stop the registration process as this plugin is foreground only. + // We stop the registration process as this plugin is foreground only. Also, if the sdk is + // less than 21 (min sdk for Camera2) we don't register the plugin. return; } + final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/camera"); @@ -126,8 +128,8 @@ public void onMethodCall(MethodCall call, final Result result) { cameras.add(details); } result.success(cameras); - } catch (CameraAccessException e) { - result.error("cameraAccess", e.getMessage(), null); + } catch (Exception e) { + handleException(e, result); } break; case "initialize": @@ -168,8 +170,8 @@ public void onMethodCall(MethodCall call, final Result result) { try { camera.startPreviewWithImageStream(); result.success(null); - } catch (CameraAccessException e) { - result.error("CameraAccess", e.getMessage(), null); + } catch (Exception e) { + handleException(e, result); } break; } @@ -178,8 +180,8 @@ public void onMethodCall(MethodCall call, final Result result) { try { camera.startPreview(); result.success(null); - } catch (CameraAccessException e) { - result.error("CameraAccess", e.getMessage(), null); + } catch (Exception e) { + handleException(e, result); } break; } @@ -198,6 +200,18 @@ public void onMethodCall(MethodCall call, final Result result) { } } + // We move catching CameraAccessException out of onMethodCall because it causes a crash + // on plugin registration for sdks incompatible with Camera2 (< 21). We want this plugin to + // to be able to compile with <21 sdks for apps that want the camera and support earlier version. + @SuppressWarnings("ConstantConditions") + private void handleException(Exception exception, Result result) { + if (exception instanceof CameraAccessException) { + result.error("CameraAccess", exception.getMessage(), null); + } + + throw (RuntimeException) exception; + } + private static class CompareSizesByArea implements Comparator { @Override public int compare(Size lhs, Size rhs) { diff --git a/packages/camera/pubspec.yaml b/packages/camera/pubspec.yaml index aee83b1e5fc6..f6b7ab5786d2 100644 --- a/packages/camera/pubspec.yaml +++ b/packages/camera/pubspec.yaml @@ -2,7 +2,7 @@ name: camera description: A Flutter plugin for getting information about and controlling the camera on Android and iOS. Supports previewing the camera feed, capturing images, capturing video, and streaming image buffers to dart. -version: 0.5.0 +version: 0.5.1 authors: - Flutter Team - Luigi Agosti