Почему это не позволит мне сделать снимок с помощью контроллера камеры?

#flutter #dart #android-camera

#flutter #dart #android-камера

Вопрос:

Я пытаюсь создать приложение, используя полноэкранную камеру. Прямая трансляция с камеры работает так же, как и обратная связь с кнопкой. Похоже, что в _controller.takePicture(path) строке есть проблема, поскольку оператор печати «IS INITIALIZED» возвращает true. Я поместил путь, поставщика путей и зависимости камеры в pubspec.yaml.

Инициализация CameraController

 cameras = await availableCameras();
    var frontCamera = cameras.first;

    _controller = CameraController(frontCamera, ResolutionPreset.high);
    try {
      await _controller.initialize();
    } catch (e) {
    }
    print('Controller Is Init:'   _controller.value.isInitialized.toString());
    displayPreview();
  
 child: GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  child:Container(
                  child: Icon(Icons.camera,color: Colors.white,size: 60,)),
                  onTap: () async {
                    print('IS INITIALIZED' _controller.value.isInitialized.toString());
                    try {
                      await _controller.takePicture(path);
                    }
                    catch(e){
                      print('E:' e);
                    }
  

Ошибка:

 E/MethodChannel#plugins.flutter.io/camera(28109): Failed to handle method call
E/MethodChannel#plugins.flutter.io/camera(28109): java.lang.NullPointerException
E/MethodChannel#plugins.flutter.io/camera(28109):   at java.io.File.<init>(File.java:283)
E/MethodChannel#plugins.flutter.io/camera(28109):   at io.flutter.plugins.camera.Camera.takePicture(Camera.java:227)
E/MethodChannel#plugins.flutter.io/camera(28109):   at io.flutter.plugins.camera.MethodCallHandlerImpl.onMethodCall(MethodCallHandlerImpl.java:77)
E/MethodChannel#plugins.flutter.io/camera(28109):   at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:230)
E/MethodChannel#plugins.flutter.io/camera(28109):   at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#plugins.flutter.io/camera(28109):   at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:664)
E/MethodChannel#plugins.flutter.io/camera(28109):   at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/camera(28109):   at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/camera(28109):   at android.os.Looper.loop(Looper.java:197)
E/MethodChannel#plugins.flutter.io/camera(28109):   at android.app.ActivityThread.main(ActivityThread.java:8125)
E/MethodChannel#plugins.flutter.io/camera(28109):   at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/camera(28109):   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
E/MethodChannel#plugins.flutter.io/camera(28109):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
E/flutter (28109): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: type 'CameraException' is not a subtype of type 'String'
E/flutter (28109): #0      _VisualPageState.build.<anonymous closure> (package:ourearth2020/screens/VisualPage.dart:209:36)
E/flutter (28109): <asynchronous suspension>
E/flutter (28109): #1      _VisualPageState.build.<anonymous closure> (package:ourearth2020/screens/VisualPage.dart)
E/flutter (28109): #2      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
E/flutter (28109): #3      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
E/flutter (28109): #4      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
E/flutter (28109): #5      BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:219:7)
E/flutter (28109): #6      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:477:9)
E/flutter (28109): #7      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:78:12)
E/flutter (28109): #8      PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:124:9)
E/flutter (28109): #9      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter (28109): #10     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:122:18)
E/flutter (28109): #11     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:108:7)
E/flutter (28109): #12     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:220:19)
E/flutter (28109): #13     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
E/flutter (28109): #14     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
E/flutter (28109): #15     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
E/flutter (28109): #16     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
E/flutter (28109): #17     _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter (28109): #18     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter (28109): #19     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter (28109): #20     _invoke1 (dart:ui/hooks.dart:283:10)
E/flutter (28109): #21     _dispatchPointerDataPacket (dart:ui/hooks.dart:192:5)
  

Комментарии:

1. Кроме того, вместо сохранения пути я могу просто сохранить его в файле, а затем просто отобразить это?

Ответ №1:

Похоже, что path вы предоставляете в качестве параметра не String , а CameraException .

Чтобы создать путь, вы можете сделать это (требуется path_provider) :

 final path = (await getTemporaryDirectory()).path   '${DateTime.now()}.png';